Skip to content

Commit

Permalink
Cache implementation passing unit and acceptance tests. This resolves #9
Browse files Browse the repository at this point in the history
, resolves #1, resolves #8
  • Loading branch information
Mark committed Jun 17, 2014
1 parent 7dcb8a7 commit 3e05f05
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
23 changes: 18 additions & 5 deletions Collatz.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(self,size):

def write(self,i,val):
try:
assert i > 0
if (self.cache[i-1] > 0): #then already wrote val
return

Expand All @@ -31,19 +32,20 @@ def write(self,i,val):
except IndexError:
pass


# -------------
# read_cache
# -------------

def read(self,i):
try:
assert i > 0
return self.cache[i-1]

except IndexError:
return 0

def size():
def size(self):
return len(self.cache)


Expand Down Expand Up @@ -93,23 +95,33 @@ def collatz_eval (i, j) :
assert i <= k

#initialize a cold cache
cache = Cache(k)
assert cache.size() == k

while( k >= i):
result=cycle_length(k)
result=cycle_length(k,cache)
if (c_len < result) :
c_len=result
k-=1
assert c_len >0
assert c_len > 0
return c_len

# -------------
# cycle_length
# -------------

def cycle_length (n) :
def cycle_length (n,cache) :
assert n > 0
c = 1
m = n

while n > 1 :
x = cache.read(n)
if (x > 0):
c = (c + x) - 1
assert c > 0
cache.write(m,c)
return c

if (n % 2) == 0 :
n = (n >> 1)
Expand All @@ -118,6 +130,7 @@ def cycle_length (n) :
n = n + (n>>1) + 1
c += 2
assert c > 0
cache.write(m,c)
return c

# -------------
Expand Down
45 changes: 44 additions & 1 deletion mas5774-TestCollatz.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,43 @@
from io import StringIO
from unittest import main, TestCase

from Collatz import collatz_read, collatz_eval, collatz_print, collatz_solve
from Collatz import Cache, collatz_read, collatz_eval, collatz_print, collatz_solve

# -----------
# TestCollatz
# -----------

class TestCollatz (TestCase) :
# ----
# Cache
# ----

def test_cache_1 (self) :
c = Cache(10)
self.assertEqual(10, c.size())


def test_cache_2 (self) :
c = Cache(10)
c.write(2, 10)
self.assertEqual(c.read(2), 10)


def test_cache_3 (self) :
c = Cache(0)
self.assertEqual(c.size(), 0)


def test_cache_4 (self) :
c = Cache(10)
c.write(1, 100)
self.assertEqual(c.read(1), 100)


def test_cache_5 (self) :
c = Cache(10)
self.assertEqual(c.read(3), 0)

# ----
# read
# ----
Expand Down Expand Up @@ -111,8 +141,21 @@ def test_eval_9 (self) :
def test_eval_10 (self) :
v = collatz_eval(20, 20)
self.assertEqual(v, 8)

def test_eval_11 (self) :
w = collatz_eval(19, 19)
self.assertEqual(w, 21)
def test_eval_12 (self) :
w = collatz_eval(1, 999999)
self.assertEqual(w, 525)

def test_eval_13 (self) :
w = collatz_eval(999999, 1)
self.assertEqual(w, 525)

def test_eval_14 (self) :
w = collatz_eval(999999, 999999)
self.assertEqual(w, 259)

# -----
# print
Expand Down

0 comments on commit 3e05f05

Please sign in to comment.