Skip to content

Instantly share code, notes, and snippets.

@kisielk
Last active January 3, 2016 22:49
Show Gist options
  • Save kisielk/8531212 to your computer and use it in GitHub Desktop.
Save kisielk/8531212 to your computer and use it in GitHub Desktop.
def E(h, n):
"""
Generates a euclidian rhythm.
Arguments:
k : The number of ones in the sequence.
n : The number of steps in the sequence.
See http://cgm.cs.mcgill.ca/~godfried/publications/banff.pdf
"""
if h > n:
raise ValueError("h must be <= n")
# The number of elements in the second group
t = n-h
head = [1]
tail = [0]
while True:
if t > h:
t = t - h
head = head + tail
else:
h, t = t, h - t
head, tail = head + tail, head
if t <= 1:
break
return ''.join(['X' if a else '.' for a in head * h + tail * t])
def test(fn):
"""
tests = [
"""
tests = [
((1, 2), 'X.'),
((1, 3), 'X..'),
((1, 4), 'X...'),
((4, 12), 'X..X..X..X..'),
((2, 3), 'X.X'),
((2, 5), 'X.X..'),
((3, 4), 'X.XX'),
((3, 5), 'X.X.X'),
((3, 7), 'X.X.X..'),
((3, 8), 'X..X..X.'),
((4, 7), 'X.X.X.X'),
((4, 9), 'X.X.X.X..'),
((4, 11), 'X..X..X..X.'),
((4, 16), 'X...X...X...X...'),
((5, 6), 'X.XXXX'),
((5, 7), 'X.XX.XX'),
((5, 8), 'X.XX.XX.'),
((5, 9), 'X.X.X.X.X'),
((5, 11), 'X.X.X.X.X..'),
((5, 12), 'X..X.X..X.X.'),
((5, 16), 'X..X..X..X..X...'),
((7, 8), 'X.XXXXXX'),
((7, 12), 'X.XX.X.XX.X.'),
((7, 16), 'X..X.X.X..X.X.X.'),
((9, 16), 'X.XX.X.X.XX.X.X.'),
((11, 24), 'X..X.X.X.X.X..X.X.X.X.X.'),
((13, 24), 'X.XX.X.X.X.X.XX.X.X.X.X.'),
]
passed = failed = 0
for test in tests:
inp = test[0]
exp = test[1]
try:
out = fn(*inp)
assert out == exp
passed += 1
except AssertionError:
failed += 1
print '%s != %s: %s' % (inp, exp, out)
print "%s passed, %s failed" % (passed, failed)
if __name__ == "__main__":
test(E)
#test(E1)
#test(E2)
E(13, 24):
13, 11 - [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
11, 2 - [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10], [1, 1]
2, 9 - [101, 101], [10, 10, 10, 10, 10, 10, 10, 10, 10]
2, 7 - [10110, 10110], [10, 10, 10, 10, 10, 10, 10]
2, 5 - [1011010, 1011010], [10, 10, 10, 10, 10]
2, 3 - [101101010, 101101010], [10, 10, 10]
2, 1 - [10110101010, 10110101010], [10]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment