Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solutions to both parts of the recursion learn lesson problem sets #226

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
38 changes: 31 additions & 7 deletions part-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,41 @@
# the appropriate comment.

# factorial


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

if n == 0:
return 1


return n * factorial(n - 1)

# reverse


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

# recursive case
length = len(text) - 1

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

# bunny


def bunny(count):
if not count:
return count

return 2 + bunny(count - 1)

# is_nested_parens

def is_nested_parens(parens):
if not parens:
return True

if parens[0] == '(' and parens[-1] == ')':
return is_nested_parens(parens[1:-1])
else:
return False

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

# search

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

if array[0] != query:
array = array[1:]
return search(array, query)
else:
return True


# is_palindrome

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

if text[0] == text[-1]:
return is_palindrome(text[1:(len(text) -1)])
else:
return False


# digit_match

def digit_match(num1, num2):

if not num1 and not num2:
return 1

else:
result = 0
last1 = num1 % 10
last2 = num2 % 10

if last1 == last2:
result += 1

if num1 // 10 == 0 or num2 // 10 == 0:
return result

return result + digit_match(num1 // 10, num2 // 10)