Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions part-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,35 @@
# the appropriate comment.

# factorial

def factorial(n):
if n < 0:
raise ValueError("Only positive numbers")
if n == 0:
return 1
return n * factorial(n - 1)


# reverse

def reverse(text):
if len(text) <= 1:
return text
return reverse(text[1:]) + text[0]


# bunny

def bunny(count):
if count == 0:
return 0
return bunny(count - 1) + 2


# is_nested_parens

def is_nested_parens(parens):
if parens == "":
return True
if len(parens) % 2 != 0:
return False
if parens[0] == "(" and parens[-1] == ")":
return is_nested_parens(parens[1:-1])
return False

32 changes: 30 additions & 2 deletions part-2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,41 @@
# the appropriate comment.

# search

def search(array, query):
if not array:
return False
if array[0] == query:
return True
return search(array[1:], query)


# is_palindrome

def is_palindrome(text):
if len(text) <= 1:
return True
if text[0] != text[-1]:
return False

return is_palindrome(text[1:-1])


# digit_match
def digit_match(num1, num2):
if num1 < 0 or num2 < 0:
raise ValueError("Numbers must be non-negative")

if num1 == 0 and num2 == 0:
return 1

elif num1 < 10 or num2 < 10:
if num1 % 10 == num2 % 10:
return 1
return 0

if num1 % 10 == num2 % 10:
return 1 + digit_match(num1 // 10, num2 // 10)

return digit_match(num1 // 10, num2 // 10)