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 sum_binary(self, a: str, b: str) -> str: | |
# sort pythonic version | |
# return str(bin(int(a, 2) + int(b, 2)))[2:] | |
# long imperative version | |
if len(a) == 1 and a[0] == '0' and len(b) == 1 and b[0] == '0': | |
return '0' | |
m_len = max(len(a), len(b)) |
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 selection_sort(nums): | |
for i in range(len(nums)): | |
minim_index = i | |
for j in range(i + 1, len(nums)): | |
if nums[minim_index] > nums[j]: | |
minim = nums[j] | |
minim_index = j | |
nums[i], nums[minim_index] = nums[minim_index], nums[i] | |
return nums | |
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 bubble_sort(arr): | |
for i in range(len(arr)): | |
is_sorted = False | |
for j in range(i, len(arr)): | |
if arr[i] > arr[j]: | |
temp = arr[j] | |
arr[j] = arr[i] | |
arr[i] = temp | |
if is_sorted: | |
break |
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
nums = [2, 3, 5, 6, 1, 4, 8, 7] | |
def quick_sort(array): # O(n log n) | |
if len(array) < 2: | |
return array | |
pivot = array[0] | |
left_arr = [i for i in array[1:] if i <= pivot] | |
right_arr = [i for i in array[1:] if i > pivot] | |
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 find_max(nums): | |
if not nums: | |
return 0 | |
return max(nums[0], find_max(nums[1:])) | |
nums = [1, 2, 3, 4, 5] | |
print(find_max(nums)) |
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 sum_nums(nums): | |
if not nums: | |
return 0 | |
return nums[0] + sum_nums(nums[1:]) | |
nums = [1, 2, 3, 4, 5] | |
print(sum_nums(nums)) |
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 count(items): | |
if not items: | |
return 0 | |
return 1 + count(items[1:]) | |
nums = [0, 1, 5, 9, 22, 33, 44, 88, 333, 555, 666, 999] | |
result = count(nums) | |
print(result) | |
print(len(nums)) |
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 find_min(nums): | |
lowest = nums[0] | |
lowest_index = 0 | |
for i in range(len(nums)): | |
if lowest > nums[i]: | |
lowest = nums[i] | |
lowest_index = i | |
return lowest_index | |
def find_max(nums): |
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 binary_search_iterative(nums, target): | |
low = 0 | |
high = len(nums) - 1 | |
while low <= high: | |
middle = (low + high) // 2 | |
curr_item = nums[middle] | |
if curr_item == target: | |
return True | |
elif curr_item > target: | |
high = middle - 1 |
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 get_input(message: str) -> float: | |
'''Prompt user for input and handle non-float ValueError conversion.''' | |
while True: | |
try: | |
degrees = float(input(message)) | |
return degrees | |
except ValueError as err: | |
print('Enter a integer or float value!!!') |
NewerOlder