Skip to content

Instantly share code, notes, and snippets.

@niloct
Created October 26, 2020 05:21
Show Gist options
  • Save niloct/b1d2ff71740b49edc2a0f5e581aca39d to your computer and use it in GitHub Desktop.
Save niloct/b1d2ff71740b49edc2a0f5e581aca39d to your computer and use it in GitHub Desktop.
from functools import lru_cache
def flip(text):
if not text:
return ''
return text[-1] + flip(text[:-1])
def addingUpTo(numbers, idx):
try:
if idx >= 0:
return numbers[idx] + addingUpTo(numbers, idx-1)
else:
return 0
except IndexError:
return addingUpTo(numbers, idx-1)
def maxOf(numbers):
if numbers:
submax = maxOf(numbers[:-1])
if submax is None:
return numbers[-1]
return max(numbers[-1], submax)
else:
return None
def includesNumber(numbers, num):
if numbers:
if numbers[-1] == num:
return True
else:
return includesNumber(numbers[:-1], num)
else:
return False
def isPalindrome(text):
if text:
return text[0] == text[-1] and isPalindrome(text[1:-1])
else:
return True
@lru_cache
def permutations(text):
if len(text) >= 2:
p = []
for idx, letter in enumerate(text):
subtext = text[0:idx] + text[idx+1:]
subps = permutations(subtext)
for subp in subps:
if not (letter + subp) in p:
p.append(letter + subp)
return p
else:
return [text]
@lru_cache
def fib(n):
if n == 0 or n == 1:
return n
return fib(n-1) + fib(n-2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment