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

# factorial
def factorial(n):
if n < 0:
raise ValueError("Factorial is not defined for negative numbers.")

if n == 0 or n == 1:
return 1

result = n * factorial(n - 1)
return result


# reverse
def reverse(text):
if len(text) <= 1:
return text

result = text[-1] + reverse(text[:-1])
return result


# bunny
def bunny(count):
if count < 0:
raise ValueError("The count of bunny should not be negative.")
if count == 0:
return 0

count = 2 + bunny(count - 1)
return count


# is_nested_parens


def is_nested_parens(parens):
if len(parens) == 0:
return True
if parens[0] != "(" or parens[-1] != ")":
return False
result = is_nested_parens(parens[1:-1])
return result
22 changes: 20 additions & 2 deletions part-2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,31 @@
# 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(n, m):
if n == 0 and m == 0:
return 1

count = 1 if n % 10 == m % 10 else 0

if n < 10 or m < 10:
return count

return count + digit_match(n // 10, m // 10)