Skip to content

Instantly share code, notes, and snippets.

@christianp
Created July 30, 2024 08:35
Show Gist options
  • Save christianp/975590c22008079d08e1bdd8771c66fd to your computer and use it in GitHub Desktop.
Save christianp/975590c22008079d08e1bdd8771c66fd to your computer and use it in GitHub Desktop.
It just makes the natural numbers
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