Skip to content

Instantly share code, notes, and snippets.

@ioanzicu
ioanzicu / sum_binary.py
Last active August 11, 2022 14:39
67. Add Binary. Given two binary strings a and b, return their sum as a binary string.
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))
@ioanzicu
ioanzicu / selection_sort.py
Created August 5, 2022 11:41
Selections sort in Python 3 - classic implementation.
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
@ioanzicu
ioanzicu / bubble_sort.py
Created August 1, 2022 14:38
Bubble Sort implementation in Python 3.
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
@ioanzicu
ioanzicu / quick_sort.py
Created July 31, 2022 17:25
Quick sort algorithm implementation in Python 3. Average/Best complexity - O(n log n), worst case - O(n**2).
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]
@ioanzicu
ioanzicu / find_max_recursive.py
Created July 28, 2022 05:47
Find the maximum value element in a list recursively in Python 3.
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))
@ioanzicu
ioanzicu / sum_recursive.py
Created July 28, 2022 05:45
Get the sum of numbers in a list recursively in Python 3.
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))
@ioanzicu
ioanzicu / count_items_recursive.py
Created July 28, 2022 05:43
Count items in a list recursive in Python 3
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))
@ioanzicu
ioanzicu / selection_sort.py
Created July 28, 2022 05:24
Implementation of Selection Sort in Python 3 with increasing or decreasing order of the items.
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):
@ioanzicu
ioanzicu / binary_search.py
Created July 28, 2022 05:10
Binary Search iterative and recursive versions implementation in Python 3.
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
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!!!')