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

# factorial

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

return n * factorial(n - 1)


# 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] != "(" or parens[-1] != ")":
return False

return is_nested_parens(parens[1:-1])

# Challenge is_nested_parens
def is_nested_parens(parens):
def check_first_and_last(start, end):
if start > end:
return True
if parens[start] != '(' or parens[end] != ')':
return False
return check_first_and_last(start + 1, end - 1)

return check_first_and_last(0, len(parens) - 1)

52 changes: 50 additions & 2 deletions part-2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,61 @@
# the appropriate comment.

# search

def search(array, query):
def check_one_element(index):
if index == len(array):
return False
elif array[index] == query:
return True

return check_one_element(index+1)

return check_one_element(0)


# is_palindrome

def is_palindrome(text):
def check_chars(for_index, back_index):
if for_index >= back_index:
return True
elif text[for_index] != text[back_index]:
return False

return check_chars(for_index+1, back_index-1)

return check_chars(0, len(text)-1)


# digit_match
def digit_match(num_1, num_2):
num_1 = str(num_1)
num_2 = str(num_2)

def count_matches(index_1, index_2, counter):
if index_1 < 0 or index_2 < 0:
return counter

if num_1[index_1] == num_2[index_2]:
counter += 1

return count_matches(index_1 - 1, index_2 - 1, counter)

return count_matches(len(num_1) - 1, len(num_2) - 1, 0)

# digit match without casting int to str
def digit_match(num_1, num_2):
if num_1 == 0 and num_2 == 0:
return 1

def count_matches(n1, n2):
if n1 == 0 or n2 == 0:
return 0

if n1 % 10 == n2 % 10:
count = 1
else:
count = 0

return count + count_matches(n1 // 10, n2 // 10)

return count_matches(num_1, num_2)