Skip to content

Commit

Permalink
Crunch day prep session
Browse files Browse the repository at this point in the history
  • Loading branch information
katieamazing committed Apr 13, 2017
1 parent 0cf2faa commit 1e3465e
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.pyc
__pycache__
.cache
.hypothesis
25 changes: 25 additions & 0 deletions Python_ChineseRemainderTheorem/remainder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Python 2.7
# Source http://rosettacode.org/wiki/Chinese_remainder_theorem#Python
# Read more: https://en.wikipedia.org/wiki/Chinese_remainder_theorem


def chinese_remainder(n, a):
sum = 0
prod = reduce(lambda a, b: a*b, n)

for n_i, a_i in zip(n, a):
p = prod / n_i
sum += a_i * mul_inv(p, n_i) * p
return sum % prod


def mul_inv(a, b):
b0 = b
x0, x1 = 0, 1
if b == 1: return 1
while a > 1:
q = a / b
a, b = b, a%b
x0, x1 = x1 - q * x0, x0
if x1 < 0: x1 += b0
return x1
16 changes: 16 additions & 0 deletions Python_ChineseRemainderTheorem/remainder_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import unittest
from remainder import chinese_remainder

class ChineseRemainderTheoremTests(unittest.TestCase):

def test_pairs(self):
self.assertEqual(chinese_remainder([7,34], [3,28]), 164)

def test_base(self):
self.assertEqual(chinese_remainder([3,5,7], [2,3,2]), 23)

def test_another(self):
self.assertEqual(chinese_remainder([3,4,5], [2,3,1]), 11)

def test_primes(self):
self.assertEqual(chinese_remainder([2,3,5,7,11], [1,1,1,1,1]), 1)
50 changes: 50 additions & 0 deletions Python_GameofLife/gameoflife.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Source: http://rosettacode.org/wiki/Conway%27s_Game_of_Life#Python
# Read more: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
# Play with this "zero-player game", with some graphics: http://conwaylife.appspot.com/

import random
from collections import defaultdict


celltable = defaultdict(int, {
(1, 2): 1,
(1, 3): 1,
(0, 3): 1,
} ) # Only need to populate with the keys leading to life

def sim(maxgenerations, universe, cellcount=(10,10)):
for i in range(maxgenerations):
## for debug printing:
# print "\nGeneration %3i:" % ( i, )
# print return_universe(universe)
nextgeneration = defaultdict(int)
for row in range(cellcount[1]):
for col in range(cellcount[0]):
nextgeneration[(row,col)] = celltable[
( universe[(row,col)],
-universe[(row,col)] + sum(universe[(r,c)]
for r in range(row-1,row+2)
for c in range(col-1, col+2) )
) ]
universe = nextgeneration
return universe

def return_universe(universe):
printdead, printlive = '-#'
output = []
for row in range(cellcount[1]):
output.append(''.join(str(universe[(row,col)])
for col in range(cellcount[0])).replace(
'0', printdead).replace('1', printlive))

return '\n'.join(output)



if __name__ == '__main__':
## sample starting conditions
maxgenerations = 3
cellcount = 10,10
# blinker
u = universe = defaultdict(int)
u[(1,0)], u[(1,1)], u[(1,2)] = 1,1,1
75 changes: 75 additions & 0 deletions Python_GameofLife/gameoflife_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import unittest
from collections import defaultdict
from gameoflife import sim

class GameofLifeTests(unittest.TestCase):

def test_blinker(self):
u = defaultdict(int)
u[(1,0)], u[(1,1)], u[(1,2)] = 1,1,1
self.assertEqual(sim(3, u), defaultdict(int, {(7, 3): 0, (6, 9): 0,
(0, 7): 0, (1, 6): 0, (3, 7): 0, (2, 5): 0, (8, 5): 0, (5, 8): 0, (4, 0): 0,
(9, 0): 0, (6, 7): 0, (5, 5): 0, (7, 6): 0, (0, 4): 0, (1, 1): 1, (3, 2): 0,
(2, 6): 0, (8, 2): 0, (4, 5): 0, (9, 3): 0, (6, 0): 0, (7, 5): 0, (0, 1): 1,
(3, 1): 0, (9, 9): 0, (7, 8): 0, (2, 1): 1, (8, 9): 0, (9, 4): 0, (5, 1): 0,
(7, 2): 0, (1, 5): 0, (3, 6): 0, (2, 2): 0, (8, 6): 0, (4, 1): 0, (9, 7): 0,
(6, 4): 0, (5, 4): 0, (7, 1): 0, (0, 5): 0, (1, 0): 0, (0, 8): 0, (3, 5): 0,
(2, 7): 0, (8, 3): 0, (4, 6): 0, (9, 2): 0, (6, 1): 0, (5, 7): 0, (7, 4): 0,
(0, 2): 0, (1, 3): 0, (4, 8): 0, (3, 0): 0, (2, 8): 0, (9, 8): 0, (8, 0): 0,
(6, 2): 0, (5, 0): 0, (1, 4): 0, (3, 9): 0, (2, 3): 0, (1, 9): 0, (8, 7): 0,
(4, 2): 0, (9, 6): 0, (6, 5): 0, (5, 3): 0, (7, 0): 0, (6, 8): 0, (0, 6): 0,
(1, 7): 0, (0, 9): 0, (3, 4): 0, (2, 4): 0, (8, 4): 0, (5, 9): 0, (4, 7): 0,
(9, 1): 0, (6, 6): 0, (5, 6): 0, (7, 7): 0, (0, 3): 0, (1, 2): 0, (4, 9): 0,
(3, 3): 0, (2, 9): 0, (8, 1): 0, (4, 4): 0, (6, 3): 0, (0, 0): 0, (7, 9): 0,
(3, 8): 0, (2, 0): 0, (1, 8): 0, (8, 8): 0, (4, 3): 0, (9, 5): 0, (5, 2): 0}))

def test_toad(self):
u = defaultdict(int)
u[(5,5)], u[(5,6)], u[(5,7)] = 1,1,1
u[(6,6)], u[(6,7)], u[(6,8)] = 1,1,1
self.assertEqual(sim(3, u), defaultdict(int, {(7, 3): 0, (6, 9): 0, (0, 7): 0,
(1, 6): 0, (3, 7): 0, (2, 5): 0, (8, 5): 0, (5, 8): 1, (4, 0): 0, (9, 0): 0,
(6, 7): 0, (5, 5): 1, (7, 6): 0, (0, 4): 0, (1, 1): 0, (3, 2): 0, (2, 6): 0,
(8, 2): 0, (4, 5): 0, (9, 3): 0, (6, 0): 0, (7, 5): 0, (0, 1): 0, (3, 1): 0,
(9, 9): 0, (7, 8): 0, (2, 1): 0, (8, 9): 0, (9, 4): 0, (5, 1): 0, (7, 2): 0,
(1, 5): 0, (3, 6): 0, (2, 2): 0, (8, 6): 0, (4, 1): 0, (9, 7): 0, (6, 4): 0,
(5, 4): 0, (7, 1): 0, (0, 5): 0, (1, 0): 0, (0, 8): 0, (3, 5): 0, (2, 7): 0,
(8, 3): 0, (4, 6): 1, (9, 2): 0, (6, 1): 0, (5, 7): 0, (7, 4): 0, (0, 2): 0,
(1, 3): 0, (4, 8): 0, (3, 0): 0, (2, 8): 0, (9, 8): 0, (8, 0): 0, (6, 2): 0,
(5, 0): 0, (1, 4): 0, (3, 9): 0, (2, 3): 0, (1, 9): 0, (8, 7): 0, (4, 2): 0,
(9, 6): 0, (6, 5): 1, (5, 3): 0, (7, 0): 0, (6, 8): 1, (0, 6): 0, (1, 7): 0,
(0, 9): 0, (3, 4): 0, (2, 4): 0, (8, 4): 0, (5, 9): 0, (4, 7): 0, (9, 1): 0,
(6, 6): 0, (5, 6): 0, (7, 7): 1, (0, 3): 0, (1, 2): 0, (4, 9): 0, (3, 3): 0,
(2, 9): 0, (8, 1): 0, (4, 4): 0, (6, 3): 0, (0, 0): 0, (7, 9): 0, (3, 8): 0,
(2, 0): 0, (1, 8): 0, (8, 8): 0, (4, 3): 0, (9, 5): 0, (5, 2): 0}))

def test_glider(self):
u = defaultdict(int)
u[(5,5)], u[(5,6)], u[(5,7)] = 1,1,1
u[(6,5)] = 1
u[(7,6)] = 1
self.assertEqual(sim(16, u), defaultdict(int, {(7, 3): 0, (6, 9): 0,
(0, 7): 0, (1, 6): 0, (3, 7): 0, (2, 5): 0, (8, 5): 0, (5, 8): 0, (4, 0): 0,
(9, 0): 0, (6, 7): 0, (5, 5): 0, (7, 6): 0, (0, 4): 0, (1, 1): 1, (3, 2): 1,
(2, 6): 0, (8, 2): 0, (4, 5): 0, (9, 3): 0, (6, 0): 0, (7, 5): 0, (0, 1): 0,
(3, 1): 0, (9, 9): 0, (7, 8): 0, (2, 1): 1, (8, 9): 0, (9, 4): 0, (5, 1): 0,
(7, 2): 0, (1, 5): 0, (3, 6): 0, (2, 2): 0, (8, 6): 0, (4, 1): 0, (9, 7): 0,
(6, 4): 0, (5, 4): 0, (7, 1): 0, (0, 5): 0, (1, 0): 0, (0, 8): 0, (3, 5): 0,
(2, 7): 0, (8, 3): 0, (4, 6): 0, (9, 2): 0, (6, 1): 0, (5, 7): 0, (7, 4): 0,
(0, 2): 0, (1, 3): 1, (4, 8): 0, (3, 0): 0, (2, 8): 0, (9, 8): 0, (8, 0): 0,
(6, 2): 0, (5, 0): 0, (1, 4): 0, (3, 9): 0, (2, 3): 0, (1, 9): 0, (8, 7): 0,
(4, 2): 0, (9, 6): 0, (6, 5): 0, (5, 3): 0, (7, 0): 0, (6, 8): 0, (0, 6): 0,
(1, 7): 0, (0, 9): 0, (3, 4): 0, (2, 4): 0, (8, 4): 0, (5, 9): 0, (4, 7): 0,
(9, 1): 0, (6, 6): 0, (5, 6): 0, (7, 7): 0, (0, 3): 0, (1, 2): 1, (4, 9): 0,
(3, 3): 0, (2, 9): 0, (8, 1): 0, (4, 4): 0, (6, 3): 0, (0, 0): 0, (7, 9): 0,
(3, 8): 0, (2, 0): 0, (1, 8): 0, (8, 8): 0, (4, 3): 0, (9, 5): 0, (5, 2): 0}))

def test_acorn(self):
u = defaultdict(int)
cellcount = 50,160
u[(14,13)], u[(15,15)], u[(16, 12)], u[(16,13)], u[(16,16)], u [(16,17)], u[(16,18)] = 1,1,1,1,1,1,1
count = 0
for k, v in sim(56, u, cellcount):
if v == 1:
count += 1
self.assertEqual(count, 160)

0 comments on commit 1e3465e

Please sign in to comment.