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

# factorial
def factorial(n):
if n < 0:
raise ValueError ("Cannot use negative numbers")
if n == 0:
return 1

return n * factorial(n - 1)


# reverse

def reverse(s):
str = ""
for i in s:
str = i + str
return str


# bunny

def bunny(count):
if count == 1:
return 2
return count * 2


# is_nested_parens

def is_nested_parens(parens):
while "()" in parens or "{}" in parens or '[]' in parens:
parens = parens.replace("()", "").replace('{}', "").replace('[]', "")
return parens == ''

33 changes: 30 additions & 3 deletions part-2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,40 @@
# the appropriate comment.

# search

def search(array, query):
i = 0
while len(array) > 0:
if array[i] == query:
array.remove(array[i])
return True
else:
array.remove(array[i])
return search(array, query)


# is_palindrome

def is_palindrome(text):
if len(text) < 2: return True
if text[0] != text[-1]: return False
return is_palindrome(text[1:-1])


# digit_match

def digit_match(apples, oranges):
a_str = str(apples)
b_str = str(oranges)

if not a_str or not b_str:
return 0

a_last = a_str[-1]
b_last = b_str[-1]

a_rest = a_str[0:-1]
b_rest = b_str[0:-1]

if a_last == b_last:
return 1 + digit_match(a_rest, b_rest)
else:
return digit_match(a_rest, b_rest)