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
def blast_off(n): | |
if n == 0: | |
print("Blastoff 🚀") | |
else: | |
print(n) | |
blast_off(n-1) | |
blast_off(5) |
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
def fib(n): | |
if n == 0 or n == 1: | |
return n | |
else: | |
return fib(n-1) + fib(n-2) | |
fib(7) |
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
def my_gcd(a, b): | |
if b == 0: | |
return abs(a) | |
else: | |
return my_gcd(b, a%b) | |
# test | |
my_gcd(0, 0) | |
my_gcd(1, 1) | |
my_gcd(12, 8) |
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
def len_list(seq): | |
if seq == []: | |
return 0 | |
else: | |
return 1 + len_list(seq[:-1]) | |
len_list([1, 2, 3]) |
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
def len_list(seq): | |
if seq == []: | |
return 0 | |
else: | |
return 1 + len_list(seq[:-1]) | |
len_list([1, 2, 3]) |
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
def reverse_string(string: str) -> str: | |
if string == "": | |
return "" | |
else: | |
return string[-1] + reverse_string(string[:-1]) | |
reverse_string("stressed") |
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
def bound_value(value: float, min_value: float, max_value: float) -> float: | |
return min(max_value, max(value, min_value)) | |
bound_value(235, min_value=0, max_value=255) |
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
def fib(n: int) -> List[int]: | |
fib_seq = [0, 1] | |
while len(fib_seq) < n: | |
fib_seq.append(sum(fib_seq[-2:])) | |
return fib_seq |
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
def my_gcd(a: int, b: int) -> int: | |
while b: | |
a, b = b, a % b | |
return abs(a) | |
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
def constrained_sum(nums: List[int]) -> int: | |
current, previous = 0, 0 | |
for value in nums: | |
previous, current = current, max(current, previous + value) | |
return current |
OlderNewer