This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)); | |
}; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |