Last active
April 22, 2021 09:05
-
-
Save Pablito2020/ce6c8d000446986f145274b6996e744b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# check whether a list is palindrome or not | |
def is_palindrome(arr: list): | |
if not arr: | |
return True | |
return arr[0] == arr[-1] and is_palindrome(arr[1:-1]) | |
def final_is_palindrome(arr: list, result=True): | |
if not arr: | |
return result | |
return final_is_palindrome(arr[1:-1], arr[0] == arr[-1] and result) | |
def iterative_palindrome(arr: list): | |
res = arr | |
result = True | |
while res: | |
if res[0] != res[-1]: | |
result = False | |
res = res[1:-1] | |
return result | |
# Check if contains an element | |
def contains_element(arr: list, num: int) -> bool: | |
if not arr: | |
return False | |
return arr[0] == num or contains_element(arr[1:], num) | |
def final_contains_element(arr: list, num: int, contains=False) -> bool: | |
if not arr: | |
return contains | |
return final_contains_element(arr[1:], num, arr[0] == num or contains) | |
def iterative_contains(arr: list, num): | |
contains = False | |
while arr: | |
contains = arr[0] == num or contains | |
arr = arr[1:] | |
return contains | |
# Reverse a list | |
def reverse(arr: list) -> list: | |
if not arr: | |
return [] | |
return [arr[-1]] + reverse(arr[0:-1]) | |
def final_reverse(arr: list, final_list=[]) -> list: | |
if not arr: | |
return final_list | |
return final_reverse(arr[0:-1], final_list + [arr[-1]]) | |
def iterative_reverse(arr: list) -> list: | |
final_list = [] | |
while arr: | |
final_list.append(arr[-1]) | |
arr = arr[0:-1] | |
return final_list | |
# Given a list, count odd numbers | |
def is_odd(num: int): | |
return num % 2 == 1 | |
def count_odd(arr: list) -> int: | |
if not arr: | |
return 0 | |
counter = 1 if is_odd(arr[0]) else 0 | |
return counter + count_odd(arr[1:]) | |
def final_count_odd(arr: list, counter=0) -> int: | |
if not arr: | |
return counter | |
return final_count_odd(arr[1:], 1 + counter if is_odd(arr[0]) else counter) | |
def iterative_count_odd(arr: list) -> int: | |
counter = 0 | |
while arr: | |
counter = 1 + counter if is_odd(arr[0]) else counter | |
arr = arr[1:] | |
return counter | |
# Check whether two lists are equal | |
def equals(first: list, second: list) -> bool: | |
if len(first) != len(second): # Improve better case running time | |
return False | |
if not first and not second: # second isn't necessary, but just for readability | |
return True | |
return first[0] == second[0] and equals(first[1:], second[1:]) | |
def final_equals(first: list, second: list, are_equal=True) -> bool: | |
if len(first) != len(second): # Improve better case running time | |
return False | |
if not first and not second: # second isn't necessary, but just for readability | |
return are_equal | |
return final_equals(first[1:], second[1:], first[0] == second[0] and are_equal) | |
def iterative_equals(first: list, second: list) -> bool: | |
if len(first) != len(second): # Improve better case running time | |
return False | |
equal = True | |
while first and second: | |
equal = first[0] == second[0] and equal | |
first = first[1:] | |
second = second[1:] | |
return equal | |
# check whether a list is cap i cua | |
def is_cap_i_cua(arr: list, first: int, second: int) -> bool: | |
if second <= first or second - first == 1: | |
return True | |
return arr[first] == arr[second] and is_cap_i_cua(arr, first + 1, second - 1) | |
def final_is_cap_i_cua(arr: list, first: int, second: int, result=True) -> bool: | |
if second <= first or second - first == 1: | |
return result | |
return final_is_cap_i_cua(arr, first + 1, second - 1, arr[first] == arr[second] and result) | |
def iterative_cap_i_cua(arr: list, first: int, second: int): | |
result = True | |
while second <= first or second - first == 1: | |
result = arr[first] == arr[second] and result | |
first += 1 | |
second -= 1 | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment