Skip to content

Instantly share code, notes, and snippets.

@Infernio
Last active November 24, 2022 19:35
Show Gist options
  • Save Infernio/2978cea94fc53f07d0b3430829ea643c to your computer and use it in GitHub Desktop.
Save Infernio/2978cea94fc53f07d0b3430829ea643c to your computer and use it in GitHub Desktop.
Aphyr's "Reversing the technical interview" in Python
# Original: https://aphyr.com/posts/340-reversing-the-technical-interview
# Because when the interviewer went “Could you just show me, you know, a regular list? Like in Python?”,
# my first thought was "Buddy, this monstrosity could be conjured in Python too".
def cons(h, t):
return lambda b: h if b else t
def nth(l, n):
if not l: return None
elif n == 0: return l(True)
else: return nth(l(False), n - 1)
def prn_list(l):
s = '['
while l:
s += str(l(True))
l = l(False)
if l: s += ', '
print(s + ']')
def reverse(l):
r = None
while l:
r = cons(l(True), r)
l = l(False)
return r
x = cons(1, cons(2, cons(3, None)))
prn_list(x)
prn_list(reverse(x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment