diff --git a/part-1.py b/part-1.py index b7dd7b6..3af4b6f 100644 --- a/part-1.py +++ b/part-1.py @@ -5,16 +5,43 @@ # factorial - +def factorial(num): + if num < 0: + raise ValueError + if num == 0: + return 1 + return num * factorial(num - 1) # reverse +def reverse(text): + if len(text) <= 1: + return text + split_text = list(text) + last_letter = split_text.pop() + return last_letter + reverse("".join(split_text)) # bunny - +def bunny(count): + if count == 0: + return 0 + elif count == 1: + return 2 + return bunny(count - 1) + 2 # is_nested_parens - +def is_nested_parens(parens): + string_length = len(parens) + if not string_length % 2: + if not parens: + return True + elif parens[0] == "(" and parens[-1] == ")": + return is_nested_parens(parens[1:string_length - 1]) + else: + return False + + else: + return False \ No newline at end of file diff --git a/part-2.py b/part-2.py index b3b77fc..8b70768 100644 --- a/part-2.py +++ b/part-2.py @@ -5,12 +5,38 @@ # search - +def search(array, query): + if len(array): + item = array.pop() + + if item == query: + return True + else: + return search(array, query) + else: + return False # is_palindrome - +def is_palindrome(text): + if not text or len(text) == 1: + return True + elif text[0] == text[-1]: + return is_palindrome(text[1:-1]) + else: + return False # digit_match - +def digit_match(num_1, num_2): + if isinstance(num_1, int): + num_1 = str(num_1) + num_2 = str(num_2) + + if len(num_1) == 0 or len(num_2) == 0: + return 0 + + if num_1[-1] == num_2[-1]: + return 1 + digit_match(num_1[:-1], num_2[:-1]) + else: + return digit_match(num_1[:-1], num_2[:-1]) \ No newline at end of file