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

# factorial
def factorial(n):
if n < 0:
raise ValueError

if n == 0:
return 1

return factorial(n - 1) * n


# reverse
def reverse(text):
if text == "":
return ""

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 not parens:
return True

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

return is_nested_parens(parens[1:-1])
50 changes: 50 additions & 0 deletions part-2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,63 @@
# the appropriate comment.

# search
# this implementation changes the list provided
def search(array, query):
if not array:
return False

if array.pop() == query:
return True

return search(array, query)


def search_preserve_array(array, query):
if not array:
return False

if array[0] == query:
return True

return search_preserve_array(array[1:], query)


# is_palindrome
def is_palindrome(text):
if not text:
return True

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

return is_palindrome(text[1:-1])


# challenge attempt
def is_palindrome_challenge(text):
return is_palindrome_challenge_helper(text, 0, -1)


def is_palindrome_challenge_helper(text, s, e):
if s > len(text)/2:
return True

if text[s] != text[e]:
return False

return is_palindrome_challenge_helper(text, s+1, e-1)


# digit_match
def digit_match(int1, int2):
return digit_match_helper(int1, int2, 0)


def digit_match_helper(int1, int2, count):
if int1 % 10 == int2 % 10:
count += 1

if not int1 // 10 or not int2 // 10:
return count

return digit_match_helper(int1//10, int2//10, count)