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

# factorial


def factorial(n):
if n < 0:
raise ValueError("Input must be >= 0.")
if n == 0:
return 1
return n * factorial(n - 1)

# reverse


def reverse(text):
# return text[::-1]

if text == "":
return "" # base case

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 # base case

if parens[0] == '(' and parens[-1] == ')':
return is_nested_parens(parens[1:-1])

return False
44 changes: 39 additions & 5 deletions part-2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,47 @@
# the appropriate comment.

# search


def search(array, query):

'''
I: array - usorted array of strings; query - string value to find
O: True - if query in array, False otherwise
'''
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(apples, oranges):
if apples == 0 and oranges == 0:
return 1
# If one or both are 1-digit numbers
elif apples < 10 or oranges < 10:
if apples % 10 == oranges % 10:
return 1

return 0

last_digit_apples = apples % 10
last_digit_oranges = oranges % 10

match = 0
if last_digit_apples == last_digit_oranges:
match = 1

return match + digit_match(apples // 10, oranges // 10)