Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Sequence generation working with tests

  • Loading branch information...
commit 69dd38a49fa2e7f7c62ec4c8bea187f084431cc4 1 parent 62a9b49
@spmacdonald authored
Showing with 45 additions and 17 deletions.
  1. +0 −9 README
  2. +16 −7 kasner.py
  3. +29 −1 tests.py
View
9 README
@@ -1,9 +0,0 @@
-
-
-Dependencies
-============
-1. svgfig for drawing diagrams (figures/routines)
- - download from: http://code.google.com/p/svgfig/
- - tar xvfz svgfix-x.y.z.tgz
- - cd svgfig/
- - sudo python setup.py install
View
23 kasner.py
@@ -23,10 +23,22 @@ def _next_sequence(self, i):
class KasnerPath:
- def __init__(self):
+ def __init__(self, n):
+ self.n = n
self.seen_paths = set()
+ self._add_all_paths()
+
+ def __iter__(self):
+ return iter(sorted(self.seen_paths))
- def add(self, s):
+ def __len__(self):
+ return len(self.seen_paths)
+
+ def _add_all_paths(self):
+ for s in KasnerSequence(self.n):
+ self._add_path(s)
+
+ def _add_path(self, s):
rotations = [s[i:] + s[:i] for i in range(len(s))]
least_rotation = sorted(rotations)[0]
if self._valid_path(least_rotation):
@@ -48,9 +60,6 @@ def _valid_path(self, path):
if __name__ == "__main__":
- paths = KasnerPath()
- for s in KasnerSequence(int(sys.argv[1])):
- paths.add(s)
-
- print len(paths.seen_paths)
+ n = int(sys.argv[1])
+ print len(KasnerPath(n))
View
30 tests.py
@@ -1,6 +1,6 @@
import unittest
-from kasner import KasnerSequence
+from kasner import KasnerSequence, KasnerPath
class TestKasnerSequence(unittest.TestCase):
@@ -29,5 +29,33 @@ def test_5(self):
actual = list(KasnerSequence(5))
self.assertEqual(actual, expected)
+
+class TestKasnerSequence(unittest.TestCase):
+
+ def test_3(self):
+ expected = [(0, 1, 2), (0, 2, 1)]
+ actual = list(KasnerPath(3))
+ self.assertEqual(actual, expected)
+
+ def test_4(self):
+ expected = [(0, 1, 0, 2), (0, 1, 2, 1), (0, 2, 1, 2)]
+ actual = list(KasnerPath(4))
+ self.assertEqual(actual, expected)
+
+ def test_5(self):
+ expected = [(0, 1, 0, 1, 2), (0, 1, 0, 2, 1),
+ (0, 1, 2, 0, 2), (0, 1, 2, 1, 2),
+ (0, 2, 0, 2, 1), (0, 2, 1, 2, 1)]
+ actual = list(KasnerPath(5))
+ self.assertEqual(actual, expected)
+
+ def test_primes(self):
+ self.assertEqual(2, len(KasnerPath(3)))
+ self.assertEqual(6, len(KasnerPath(5)))
+ self.assertEqual(18, len(KasnerPath(7)))
+ self.assertEqual(186, len(KasnerPath(11)))
+ self.assertEqual(630, len(KasnerPath(13)))
+
+
if __name__ == "__main__":
unittest.main()
Please sign in to comment.
Something went wrong with that request. Please try again.