-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbjorklund.py
40 lines (37 loc) · 1022 Bytes
/
bjorklund.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
Copyright (c) 2011 Brian House
https://github.com/brianhouse/bjorklund
"""
def bjorklund(steps, pulses):
steps = int(steps)
pulses = int(pulses)
if pulses > steps:
raise ValueError
pattern = []
counts = []
remainders = []
divisor = steps - pulses
remainders.append(pulses)
level = 0
while True:
counts.append(divisor / remainders[level])
remainders.append(divisor % remainders[level])
divisor = remainders[level]
level = level + 1
if remainders[level] <= 1:
break
counts.append(divisor)
def build(level):
if level == -1:
pattern.append(0)
elif level == -2:
pattern.append(1)
else:
for i in xrange(0, counts[level]):
build(level - 1)
if remainders[level] != 0:
build(level - 2)
build(level)
i = pattern.index(1)
pattern = pattern[i:] + pattern[0:i]
return pattern