Created
July 30, 2024 08:35
-
-
Save christianp/975590c22008079d08e1bdd8771c66fd to your computer and use it in GitHub Desktop.
It just makes the natural numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NegativeStep(Exception): | |
pass | |
def countdown(n,step=0): | |
if n==0: | |
return [0] | |
if n<0: | |
raise NegativeStep() | |
for d in range(-1,2): | |
try: | |
s = step+d | |
if s < 0: | |
return [n]+countdown(n+s, s) | |
except NegativeStep: | |
continue | |
raise NegativeStep() | |
def runs(seq): | |
last = None | |
t = 0 | |
for x in seq: | |
if x != last: | |
if t > 0: | |
yield t | |
t = 0 | |
last = x | |
else: | |
t += 1 | |
list(runs([len(countdown(n)) for n in range(1,50)])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment