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

Recursion PS pt 1 and 2 - A19 Gloria Wan #206

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
55 changes: 50 additions & 5 deletions part-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,64 @@
# the required functions to build.
# Please paste your solution underneath
# the appropriate comment.

##############################################
# factorial
##############################################
def factorial(n):
if n == 0:
return 1
elif n < 0:
raise ValueError(f"{n} must be a positive integer")

return n*factorial(n-1)



##############################################
# reverse
##############################################
def reverse(text):
# checks to make sure the length of text is not
if len(text) == 0:
return text
# adds the first to the end by using string operation
# text[0] is in a holding area until the recursion is completed
# reverse then slices the text string starting from the value 1
return reverse(text[1:]) + text[0]




##############################################
# bunny
##############################################
def bunny(count):
if count == 0:
return 0
return bunny(count-1)+2



##############################################
# is_nested_parens
##############################################
# function checks if the length of the string is even first
def is_nested_parens(parens):
if len(parens)%2 != 0:
return False
else:
# function that will do recursion
return check_string(parens)

# helper function that has a recursive that returns the string
# without the first and last character
def check_string(parens):
# condition to break recursion
if len(parens) == 0:
return True
# recursion act
elif len(parens) > 0:
# first and last parenthesis should never be the same
if parens[0] == parens[-1]:
return False
# recursion that removes the first and last character
return check_string(parens[1:-1])
else:
return False

60 changes: 52 additions & 8 deletions part-2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,59 @@
# the required functions to build.
# Please paste your solution underneath
# the appropriate comment.

##############################################
# search



##############################################
def search(array,query):
# function will always check if the array is empty
if len(array) == 0:
return False
# the function will check if the first value against the value for query
elif array[0] == query:
return True
else:
# recursive function that removes the first value
return search(array[1:],query)

##############################################
# is_palindrome



##############################################
def is_palindrome(text):
# if the recursion empties the list it should break
if len(text) == 0:
return True
elif text[0] == text[-1]:
# recursive function that removes the first and last character
# it will keep getting called as long as the first and last character
# is the same
return is_palindrome(text[1:-1])
# if the first and last value does not equal each other
# if you have not reached the end of string
else:
return False

##############################################
# digit_match

##############################################
def digit_match(first_num, second_num):
num_match = 0
first_str = str(first_num)
second_str = str(second_num)

num_match += count_match(first_str, second_str, num_match)

return num_match

# helper function that uses recursion to keep count
def count_match (first_str, second_str,count):
# checks if we have reached the end of one integer
if len(first_str) == 0 or len(second_str) == 0:
return count
else:
# increase count if it matches
if first_str[-1] == second_str[-1]:
count += 1
# as long both strings are still populated it will remove the last
# integer for both and also pass the current count
return count_match(first_str[:-1], second_str[:-1], count)