-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a104ee4
commit 4657390
Showing
1,094 changed files
with
174,544 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
def add_binary(num1, num2): | ||
n = max(len(num1), len(num2)) | ||
num1, num2 = num1.zfill(n), num2.zfill(n) | ||
carry = 0 | ||
sum_string = '' | ||
for i in range(n - 1, -1, -1): | ||
if num1[i] == '1': | ||
carry += 1 | ||
if num2[i] == '1': | ||
carry += 1 | ||
|
||
if carry % 2 == 1: | ||
sum_string += str(1) | ||
else: | ||
sum_string += str(0) | ||
|
||
carry //= 2 | ||
|
||
if carry == 1: | ||
sum_string += str(1) | ||
|
||
return sum_string[::-1] | ||
|
||
|
||
print(add_binary("11", "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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
arr1 = [1, 2, 3, 4] | ||
arr2 = [2, 5, 6] | ||
|
||
m = len(arr1) | ||
n = len(arr2) | ||
t_length = m + n | ||
|
||
# Unpack the array and fill in the 0 padded values | ||
arr1 = [*arr1, *[0] * n] | ||
|
||
p1 = m - 1 | ||
p2 = n - 1 | ||
p = t_length - 1 | ||
|
||
while p1 >= 0 and p2 >= 0: | ||
if arr1[p1] < arr2[p2]: | ||
arr1[p] = arr2[p2] | ||
p2 -= 1 | ||
else: | ||
arr1[p] = arr1[p1] | ||
p1 -= 1 | ||
|
||
p -= 1 | ||
|
||
arr1[:p2 + 1] = arr2[:p2 + 1] | ||
|
||
|
||
print(arr1) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
def binary_search(haystack, needle): | ||
i = 0 | ||
if len(haystack) > 1: | ||
low = 0 | ||
high = len(haystack) - 1 | ||
mid = int((low + high) / 2) | ||
|
||
for number in haystack: | ||
if needle < haystack[mid]: | ||
mid -= 1 | ||
elif needle > haystack[mid]: | ||
mid += 1 | ||
else: | ||
return haystack[mid] | ||
i += 1 | ||
|
||
|
||
found = binary_search([1, 10, 20, 47, 59, 63, 75, 88, 99, 107, 120, 133, 155, 162, 176, 188, 199, 200, 210, 222], 155) | ||
|
||
print("I found the number: {} in a few step".format(found)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
class Node(object): | ||
summation = [] | ||
|
||
def __init__(self, value): | ||
self.value = value | ||
self.left = None | ||
self.right = None | ||
|
||
def insert(self, value): | ||
if self.value: | ||
if value < self.value: | ||
if self.left is None: | ||
self.left = Node(value) | ||
else: | ||
self.left.insert(value) | ||
|
||
elif value > self.value: | ||
if self.right is None: | ||
self.right = Node(value) | ||
else: | ||
self.right.insert(value) | ||
|
||
else: | ||
self.value = value | ||
|
||
def get_node_values(self): | ||
if self.left: | ||
self.left.get_node_values() | ||
self.summation.append(self.value) | ||
if self.right: | ||
self.right.get_node_values() | ||
|
||
return self.summation | ||
|
||
def find_a_value(self, value): | ||
if value < self.value: | ||
if self.left is None: | ||
return print("{} not found".format(value)) | ||
return self.left.find_a_value(value) | ||
|
||
elif value > self.value: | ||
if self.right is None: | ||
return print("{} not found".format(value)) | ||
return self.right.find_a_value(value) | ||
|
||
else: | ||
print(str(self.value) + ' is found') | ||
|
||
""" | ||
Inorder traversal, you visit the left first, then | ||
the root and finally, | ||
the right | ||
""" | ||
def in_order_traversal(self, root): | ||
sum_up = [] | ||
if root: | ||
sum_up = self.in_order_traversal(root.left) | ||
sum_up.append(root.value) | ||
sum_up += self.in_order_traversal(root.right) | ||
|
||
return sum_up | ||
|
||
""" | ||
Preorder traversal, you visit the root first, then | ||
the left and finally, | ||
the right | ||
""" | ||
def pre_order_traversal(self, root): | ||
sum_up = [] | ||
if root: | ||
sum_up.append(root.value) | ||
sum_up += self.in_order_traversal(root.left) | ||
sum_up += self.in_order_traversal(root.right) | ||
|
||
return sum_up | ||
|
||
""" | ||
Postorder traversal, you visit the left first, then | ||
the right and finally, | ||
the root | ||
""" | ||
def post_order_traversal(self, root): | ||
sum_up = [] | ||
if root: | ||
sum_up = self.in_order_traversal(root.left) | ||
sum_up += self.in_order_traversal(root.right) | ||
sum_up.append(root.value) | ||
|
||
return sum_up | ||
|
||
|
||
# root = Node(21) | ||
# root.insert(4) | ||
# root.insert(1) | ||
# root.insert(4) | ||
# root.insert(9) | ||
# root.insert(3) | ||
# root.insert(20) | ||
# root.insert(25) | ||
# root.insert(6) | ||
# root.insert(21) | ||
# root.insert(14) | ||
# | ||
# # values = root.get_node_values() | ||
# # print(values) | ||
# | ||
# print(root.in_order_traversal(root)) | ||
# print(root.pre_order_traversal(root)) | ||
# print(root.post_order_traversal(root)) | ||
# | ||
# # root.find_a_value(31) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
def bubble_sort(lists): | ||
for num in range(len(lists) - 1, 0, -1): | ||
for index in range(num): | ||
if lists[index] > lists[index + 1]: | ||
swap = lists[index] | ||
lists[index] = lists[index + 1] | ||
lists[index + 1] = swap | ||
|
||
return lists | ||
|
||
|
||
data = [21, 4, 1, 3, 9, 20, 25, 6, 21, 14] | ||
print(bubble_sort(data)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
We're the knights of the round table | ||
We dance whenever we're able |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from typing import List | ||
|
||
|
||
def kids_with_candies(candies: List[int], extra_candies: int) -> List[bool]: | ||
max_candy = max(candies) | ||
|
||
index = 0 | ||
for candy in candies: | ||
candy_and_extra = candy + extra_candies | ||
if candy_and_extra < max_candy: | ||
candies[index] = False | ||
else: | ||
candies[index] = True | ||
index += 1 | ||
|
||
return candies | ||
|
||
|
||
print(kids_with_candies(candies=[4, 2, 1, 1, 2], extra_candies=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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from binary_tree import Node | ||
|
||
|
||
def closest_binary_search_tree(node: Node, target): | ||
""" | ||
4 | ||
/ \ | ||
2 5 | ||
/ \ | ||
1 3 | ||
:return: | ||
""" | ||
# rounded_target = round(target) | ||
closest = node.value | ||
|
||
while node: | ||
closest = min(node.value, closest, key=lambda x: abs(target - x)) | ||
node = node.left if target < node.value else node.right | ||
return closest | ||
|
||
|
||
root = Node(4) | ||
root.insert(2) | ||
root.insert(5) | ||
root.insert(1) | ||
root.insert(3) | ||
|
||
print(closest_binary_search_tree(root, 3.414286)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from binary_tree import Node | ||
|
||
|
||
class Solution: | ||
def diameterOfBinaryTree(self, root: Node) -> int: | ||
node = root | ||
self.diameter = 0 | ||
|
||
def depth(node): | ||
if not node: | ||
return 0 | ||
|
||
left_height = depth(node.left) | ||
right_height = depth(node.right) | ||
# path | ||
self.diameter = max(self.diameter, left_height + right_height) | ||
# depth | ||
return max(right_height, left_height) + 1 | ||
|
||
depth(node) | ||
|
||
return self.diameter | ||
|
||
|
||
root = Node(1) | ||
root.insert(2) | ||
root.insert(3) | ||
root.insert(4) | ||
root.insert(5) | ||
|
||
sol = Solution() | ||
print(sol.diameterOfBinaryTree(root)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
def fibo_series(position): | ||
if position < 1: | ||
return position | ||
|
||
first = 0 | ||
second = 1 | ||
next_value = first + second | ||
|
||
for num in range(2, position - 1): | ||
first = second | ||
second = next_value | ||
next_value = first + second | ||
|
||
return next_value | ||
|
||
|
||
print(fibo_series(10)) | ||
|
||
|
||
def dynamic_fibo_series(position): | ||
if position < 1: | ||
return position | ||
|
||
fibo = [num for num in range(0, position)] | ||
fibo[0] = 0 | ||
fibo[1] = 1 | ||
|
||
count = 2 | ||
|
||
for num in range(2, position): | ||
fibo[count] = fibo[count - 1] + fibo[count - 2] | ||
count += 1 | ||
|
||
return fibo | ||
|
||
|
||
print(dynamic_fibo_series(10)) | ||
|
||
|
||
def recursive_fibo(position): | ||
if position == 0: | ||
return 0 | ||
if position == 1: | ||
return 1 | ||
|
||
return recursive_fibo(position - 1) + recursive_fibo(position - 2) | ||
|
||
|
||
print(recursive_fibo(10)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
def findPairsWithGivenDifference(arr, k): | ||
# since we don't allow duplicates, no pair can satisfy x - 0 = y | ||
if k == 0: | ||
return [] | ||
|
||
pair_map = {} | ||
answer = [] | ||
|
||
for x in arr: | ||
pair_map[x - k] = x | ||
|
||
for y in arr: | ||
if y in pair_map: | ||
answer.append([pair_map[y], y]) | ||
return answer | ||
|
||
|
||
print(findPairsWithGivenDifference([0, -1, -2, 2, 1], 1)) |
Oops, something went wrong.