Skip to content

Instantly share code, notes, and snippets.

@LeReverandNox
Created March 31, 2018 20:11
Show Gist options
  • Select an option

  • Save LeReverandNox/03394ebfecb9f2e0c955a02052bf63b5 to your computer and use it in GitHub Desktop.

Select an option

Save LeReverandNox/03394ebfecb9f2e0c955a02052bf63b5 to your computer and use it in GitHub Desktop.
def count_up_to_50(n):
return [] if n > 50 else [n] + count_up_to_50(n + 1)
def sum_to(n):
return n if n == 1 else n + sum_to(n - 1)
def fibonacci(nb, prev_n = 0, next_n = 1):
return [next_n] if nb == 0 else [next_n] + fibonacci(nb - 1, next_n, prev_n + next_n)
def print_list(l):
if l == []: return
print(l[0])
print_list(l[1:])
def count_digits(n, i = 1):
return i if n % (10 ** i) == n else count_digits(n, i + 1)
def sum_of_digits(n):
return 0 if n == 0 else (n % 10) + sum_of_digits(n//10)
def pgcd(n1, n2):
return n2 if n1%n2 == 0 else pgcd(n2, n1%n2)
def max_list(l, max_n = 0):
if l == []: return max_n
if l[0] > max_n: max_n = l[0]
return max_list(l[1:], max_n)
def reverse_string(string):
return string if string == "" else string[len(string) - 1] + reverse_string(string[:-1])
def fact(n):
if n <= 0: return 0
return n if n == 1 else n * fact(n-1)
# print(count_up_to_50(1))
# print(sum_to(5))
# print(fibonacci(10))
# print_list([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# print(count_digits(12345))
# print(sum_of_digits(999))
# print(pgcd(96, 36))
# print(max_list([9, 3, 13, 5]))
# print(reverse_string('Hello, World!'))
# print(fact(5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment