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
27 changes: 23 additions & 4 deletions part-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,36 @@
# the appropriate comment.

# factorial

def factorial(n):
if n < 0:
raise ValueError("Factorial is not defined for negative numbers.")
if n == 0:
return 1
return n * factorial(n - 1)


# reverse

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


# bunny

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


# is_nested_parens

def is_nested_parens(parens):
def helper(start, end):
if start > end:
return True
if parens[start] != '(' or parens[end] != ')':
return False
return helper(start + 1, end - 1)

return helper(0, len(parens) - 1)

29 changes: 28 additions & 1 deletion part-2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,40 @@
# 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):
def helper(left, right):
if left >= right:
return True
if text[left] != text[right]:
return False
return helper(left + 1, right - 1)

return helper(0, len(text) - 1)


# digit_match
def digit_match(apples, oranges):
if apples == 0 or oranges == 0:
return 1 if apples == 0 and oranges == 0 else 0

match = 0
if apples % 10 == oranges % 10:
match = 1

remaining_apples = apples // 10
remaining_oranges = oranges // 10

if remaining_apples == 0 and remaining_oranges == 0:
return match

return match + digit_match(remaining_apples, remaining_oranges)