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

# factorial

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


# 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 len(parens) == 0:
return True

if parens[0] + parens[-1] != "()":
return False
return is_nested_parens(parens[1:-1])


# Extra challenge:
def is_nested_parens(parens):
return helper(parens, 0, len(parens) - 1)

def helper(parens,left,right): # left and right are indices of first and last str char
if left > right:
return True

if parens[left] != "(" or parens[right] != ")":
return False
return helper(parens, left+1 , right-1)
25 changes: 25 additions & 0 deletions part-2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,38 @@
# the appropriate comment.

# search
def search(array,query):
if len(array) == 0:
return False

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



# is_palindrome
def is_palindrome(text):
if len(text) == 0:
return True

if text[0] != text[-1]:
return False
return is_palindrome(text[1:-1])



# digit_match
def digit_match(int1, int2):
if int1 < 0 or int2 < 0:
raise ValueError("Input must be a non-negative integer.")

if int1 == 0 and int2 == 0:
return 1

match = 1 if (int1 % 10 == int2 % 10) else 0

if int1 < 10 or int2 < 10:
return match

return match + digit_match(int1 // 10, int2 // 10)