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

# factorial
def factorial(n):
if n < 0:
raise ValueError
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 2 + bunny(count-1)



# is_nested_parens
# is_nested_parensdef is_nested_parens(parens):
def is_nested_parens(parens):
return is_nested_helper(parens, 0)



def is_nested_helper(parens, count):
if not parens:
return count == 0

if parens[0] == '(':
return is_nested_helper(parens[1:], count + 1)
elif parens[0] == ')':
if count == 0:
return False
return is_nested_helper(parens[1:], count - 1)
return False
33 changes: 32 additions & 1 deletion part-2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,44 @@
# 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(s_input):
if len(s_input) <= 1:
return True

if s_input[0] != s_input[-1]:
return False

return is_palindrome(s_input[1:-1])



# digit_match

def digit_match(number1, number2):
if number1 == 0 and number2 == 0:
return 1

elif number1 < 10 or number2 < 10:
count_pairs = 0
if number1 % 10 == number2 % 10:
count_pairs += 1
return count_pairs


count_pairs = 0
if number1 % 10 == number2 % 10:
count_pairs += 1

return count_pairs + digit_match(number1 // 10, number2 // 10)