Skip to content

Instantly share code, notes, and snippets.

View animatedlew's full-sized avatar

Lewie [m80] animatedlew

View GitHub Profile
@animatedlew
animatedlew / sqrt.js
Created November 9, 2013 04:07
Here is an implementation of a square root function in JavaScript using Newton's method.
var sqrt = function(x) {
var isGoodEnough = function(guess) {
return Math.abs(guess * guess - x) / x < 0.001;
};
var improve = function(guess) {
return (guess + x / guess) / 2;
};
var sqrIter = function(guess) {
@animatedlew
animatedlew / pascal.py
Last active December 27, 2015 20:48
An implementation of pascal's triangle in python. Please compare this to my implementation in Scala.
def pascal(r, c):
fac = lambda(num): (1 if (num < 1) else num*fac(num-1))
return fac(r)/(fac(c)*fac(r-c))
def pascal2(r, c):
return 1 if (c == 0 or c == r) else pascal2(r-1, c-1) + pascal2(r-1, c)
def triangle(totalRows, op):
def draw(op):
list = ""
@animatedlew
animatedlew / symbolbalancer.scala
Last active December 28, 2015 01:39
Iterates through a string of characters and returns true if the input contains a balanced set of parentheses.
package practice
object SymbolBalancer extends App {
val data: List[String] = List(
"(if (zero? x) max (/ 1 x))",
"I told him (that it's not (yet) done). (But he wasn't listening)",
":-)",
"())(",
"((d)o(n)e)",
@animatedlew
animatedlew / modulo.scala
Created November 16, 2013 05:45
This is my implementation of the modulo operator. The function first checks to see if the result is positive. If it is, it generates a new dividend by subtracting the divisor, thereby narrowing down to the remainder, which will be left in the dividend.
def modulo(dividend: Int, divisor: Int): Int = {
if (dividend / divisor > 0) modulo(dividend - divisor, divisor) else dividend
}
def %(money: Int, coin: Int): Int = {
money % coin
}
println(%(4, 3))
println(modulo(4, 3))
@animatedlew
animatedlew / countChange.js
Created November 18, 2013 05:18
Finding how many combinations of change can be produced based on a total amount and list of denominations. Note that the Underscore.js dependency is required for legibility.
(function(_) {
var money = 300;
var coins = [5, 10, 20, 50, 100, 200, 500];
var countChange = function(money, coins) {
if (money == 0) return 1;
else if (_.isEmpty(coins) || money < 0) return 0;
else return countChange(money - _.head(coins), coins) + countChange(money, _.tail(coins));
};
@animatedlew
animatedlew / countChange.scala
Last active December 28, 2015 15:39
Finding how many combinations of change can be produced based on a total amount and list of denominations in Scala.
val money = 300
val coins = List(5, 10, 20, 50, 100, 200, 500)
def countChange(money: Int, coins: List[Int]): Int = {
if (money == 0) 1
else if (money < 0 || coins.isEmpty) 0
else countChange(money, coins.tail) + countChange(money - coins.head, coins)
}
// 1022
@animatedlew
animatedlew / count_change.py
Last active December 28, 2015 15:39
Finding how many combinations of change can be produced based on a total amount and list of denominations in Python.
def count_change(money, coins):
if money == 0:
return 1
if not coins or money < 0:
return 0
else:
return count_change(money-coins[0], coins) + count_change(money, coins[1:])
money = 300
coins = [5, 10, 20, 50, 100, 200, 500]
@animatedlew
animatedlew / countChange.cs
Last active December 28, 2015 15:39
Finding how many combinations of change can be produced based on a total amount and list of denominations in C#.
namespace CountChange
{
class Program
{
static int CountChange(int money, int[] coins)
{
if (money == 0) return 1;
else if (money < 0 || coins.Length == 0) return 0;
else return CountChange(money - coins.First(), coins) + CountChange(money, coins.Skip(1).ToArray());
}
@animatedlew
animatedlew / calc_total_paths_spec.py
Created November 20, 2013 03:28
This is the test suite for algorithms.py
import unittest
import algorithms
class TestCalcTotalPaths(unittest.TestCase):
def setUp(self):
self.loc = {'three': (1, 2), 'two': (1, 1), 'fiftySix': (5, 3), 'nine': (4, 1)}
self.list_comp = [(x, y) for x in range(6) for y in range(4)]
@animatedlew
animatedlew / algorithms.py
Last active December 28, 2015 20:29
This function takes a set of coordinates on a grid and returns the number of discrete paths that can be taken from the origin.
def count_paths(x, y):
if x is 0 and y is 0:
return 1
else:
a = 0 if y is 0 else count_paths(x, y-1)
b = 0 if x is 0 else count_paths(x-1, y)
return a + b
destination = (5, 3)