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
28 changes: 24 additions & 4 deletions part-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,36 @@

# factorial


def factorial(n):
if n < 0:
raise ValueError
if n == 0 or n == 1:
return 1

return n * factorial(n - 1)


# reverse


def reverse(text):
if text == '':
return ''

return reverse(text[1:]) + text[0]

# bunny


def bunny(count):
if count == 0:
return 0

return 2 + bunny(count - 1)

# is_nested_parens


def is_nested_parens(parens):
if parens == "":
return True
if parens[0] == "(" and parens[-1] == ")":
return is_nested_parens(parens[1:-1])
return False
43 changes: 41 additions & 2 deletions part-2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,51 @@

# search


def search(list, query):
if list == []:
return False

if list[0] == query:
return True

return search(list[1:], query)

# is_palindrome

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

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

return is_palindrome(text[1: -1])


# digit_match


def digit_match(num1, num2):
return helper(num1, num2, 0)

def helper(n1, n2, count):
if n1 == 0 and n2 == 0:
return 1

if n1 == 0 or n2 == 0:
return count

if n1 % 10 == n2 % 10:
count += 1

return helper(n1 // 10, n2 // 10, count)

# digit_match(another impementation, but with help)

def digit_match(apples, oranges):
if apples == 0 and oranges == 0:
return 1
if apples < 10 or oranges < 10:
return 1 if (apples % 10) == (oranges % 10) else 0

match = 1 if (apples % 10) == (oranges % 10) else 0
return match + digit_match(apples // 10, oranges // 10)