Skip to content

Commit cc5690f

Browse files
committed
Formatted repo with Black.
1 parent de1072c commit cc5690f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+894
-769
lines changed

BST_nodes_in_range.py

+22-17
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
1-
# Problem: Find the no. of nodes in a BST
1+
# Problem: Find the no. of nodes in a BST
22
# that lies in a given range
33

4+
45
class Node:
5-
def __init__(self, data):
6-
self.data = data
7-
self.left = None
8-
self.right = None
6+
def __init__(self, data):
7+
self.data = data
8+
self.left = None
9+
self.right = None
10+
911

1012
def nodesWithinRange(root, range):
11-
low, high = range
12-
if root is None:
13-
return 0
14-
elif root.data <= high and root.data >= low:
15-
return 1 + nodesWithinRange(root.left, range) + nodesWithinRange(root.right, range)
16-
elif root.data == high or root.data == low:
17-
return 1
18-
elif root.data > high:
19-
return nodesWithinRange(root.left, range)
20-
elif root.data < low:
21-
return nodesWithinRange(root.right, range)
22-
13+
low, high = range
14+
if root is None:
15+
return 0
16+
elif root.data <= high and root.data >= low:
17+
return (
18+
1 + nodesWithinRange(root.left, range) + nodesWithinRange(root.right, range)
19+
)
20+
elif root.data == high or root.data == low:
21+
return 1
22+
elif root.data > high:
23+
return nodesWithinRange(root.left, range)
24+
elif root.data < low:
25+
return nodesWithinRange(root.right, range)
26+
27+
2328
node = Node(10)
2429
node.left = Node(5)
2530
node.left.left = Node(1)

binary_search_recursive.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33
# binary search. If the element is not found
44
# it returns -1
55

6+
67
def binarySearch(lst, key, l, r):
7-
if r>=l:
8-
mid = l + (r-l) // 2
9-
# use print(l, mid, r) to view the process
10-
if lst[mid] == key:
11-
return mid
12-
elif lst[mid] < key:
13-
return binarySearch(lst, key, mid+1, r)
14-
elif lst[mid] > key:
15-
return binarySearch(lst, key, l, mid-1)
16-
else:
17-
return -1
18-
8+
if r >= l:
9+
mid = l + (r - l) // 2
10+
# use print(l, mid, r) to view the process
11+
if lst[mid] == key:
12+
return mid
13+
elif lst[mid] < key:
14+
return binarySearch(lst, key, mid + 1, r)
15+
elif lst[mid] > key:
16+
return binarySearch(lst, key, l, mid - 1)
17+
else:
18+
return -1
19+
20+
1921
arr = [int(i) for i in range(101)]
20-
print(binarySearch(arr, 67, 0, len(arr)-1))
22+
print(binarySearch(arr, 67, 0, len(arr) - 1))

check_anagrams.py

+16-11
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,33 @@
44
# anagrams of each other
55

66
import string
7+
78
letters = string.ascii_lowercase
8-
CHARACTER_HASH = dict(zip(letters, [0]*len(letters)))
9+
CHARACTER_HASH = dict(zip(letters, [0] * len(letters)))
10+
911

1012
def mapLettersToHash(text_a):
11-
for char in text_a:
12-
if char in CHARACTER_HASH.keys():
13-
CHARACTER_HASH[char]+=1
13+
for char in text_a:
14+
if char in CHARACTER_HASH.keys():
15+
CHARACTER_HASH[char] += 1
16+
1417

1518
def computeCommonLetters(text_b):
16-
common_letters = 0
17-
for char in text_b:
18-
if CHARACTER_HASH[char] > 0:
19-
common_letters+=1
20-
return common_letters
19+
common_letters = 0
20+
for char in text_b:
21+
if CHARACTER_HASH[char] > 0:
22+
common_letters += 1
23+
return common_letters
24+
2125

2226
def computeUncommonLetters(text_a, text_b, common_letters):
23-
return abs(len(text_a)+len(text_b)-(2*common_letters))
27+
return abs(len(text_a) + len(text_b) - (2 * common_letters))
28+
2429

2530
text_1 = "hello"
2631
text_2 = "billion"
2732

2833
mapLettersToHash(text_1)
2934
common = computeCommonLetters(text_2)
3035
result = computeUncommonLetters(text_1, text_2, common)
31-
print(result)
36+
print(result)

check_semiprime.py

+26-24
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,30 @@
55
# Problem: Find the numbers which are semiprimes,
66
# within a given range. For e.g. 1 to 100.
77

8+
89
def isSemiprime(num):
9-
# start with the smallest prime
10-
prime = 2
11-
# initialize counter to 0
12-
count = 0
13-
# Design of while loop:
14-
# 1. if count exceeds 2, it is not a semiprime, e.g. 30 = 2*3*5
15-
# 2. when the number becomes 1, we have found the second prime
16-
while(count<3 and num!=1):
17-
# if the number is divisible by current prime,
18-
# increment count, else move to new prime
19-
if(not(num % prime)):
20-
num = num / prime
21-
count=count+1
22-
else:
23-
prime = prime + 1
24-
# if count is two, given number is a semiprime
25-
return count == 2
26-
27-
for i in range(1,100):
28-
if(isSemiprime(i)):
29-
print(i, end=' ')
30-
31-
# Result: 4 6 9 10 14 15 21 22 25 26 33 34 35 38 39 46 49
32-
# 51 55 57 58 62 65 69 74 77 82 85 86 87 91 93 94 95
10+
# start with the smallest prime
11+
prime = 2
12+
# initialize counter to 0
13+
count = 0
14+
# Design of while loop:
15+
# 1. if count exceeds 2, it is not a semiprime, e.g. 30 = 2*3*5
16+
# 2. when the number becomes 1, we have found the second prime
17+
while count < 3 and num != 1:
18+
# if the number is divisible by current prime,
19+
# increment count, else move to new prime
20+
if not (num % prime):
21+
num = num / prime
22+
count = count + 1
23+
else:
24+
prime = prime + 1
25+
# if count is two, given number is a semiprime
26+
return count == 2
27+
28+
29+
for i in range(1, 100):
30+
if isSemiprime(i):
31+
print(i, end=" ")
32+
33+
# Result: 4 6 9 10 14 15 21 22 25 26 33 34 35 38 39 46 49
34+
# 51 55 57 58 62 65 69 74 77 82 85 86 87 91 93 94 95

dfs_bfs.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# 1. Depth First Search (DFS)
44
# 2. Breadth First Search (BFS)
55

6+
67
def dfs_1(graph, start):
78
visited, stack = set(), [start]
89
while stack:
@@ -12,6 +13,7 @@ def dfs_1(graph, start):
1213
stack.extend(graph[vertex] - visited)
1314
return visited
1415

16+
1517
def dfs_2(graph, start, visited=None):
1618
if visited is None:
1719
visited = set()
@@ -20,6 +22,7 @@ def dfs_2(graph, start, visited=None):
2022
dfs_2(graph, next, visited)
2123
return visited
2224

25+
2326
def bfs(graph, start):
2427
visited, queue = set(), [start]
2528
while queue:
@@ -29,8 +32,10 @@ def bfs(graph, start):
2932
queue.extend(graph[vertex] - visited)
3033
return visited
3134

35+
3236
# bfs(graph, 'A') # {'B', 'C', 'A', 'F', 'D', 'E'}
3337

38+
3439
def dfs_paths(graph, start, goal):
3540
stack = [(start, [start])]
3641
while stack:
@@ -41,12 +46,15 @@ def dfs_paths(graph, start, goal):
4146
else:
4247
stack.append((next, path + [next]))
4348

44-
graph = {'A': set(['B', 'C']),
45-
'B': set(['A', 'D', 'E']),
46-
'C': set(['A', 'F']),
47-
'D': set(['B']),
48-
'E': set(['B', 'F']),
49-
'F': set(['C', 'E'])}
5049

51-
result = list(dfs_paths(graph, 'A', 'F')) # [['A', 'C', 'F'], ['A', 'B', 'E', 'F']]
50+
graph = {
51+
"A": set(["B", "C"]),
52+
"B": set(["A", "D", "E"]),
53+
"C": set(["A", "F"]),
54+
"D": set(["B"]),
55+
"E": set(["B", "F"]),
56+
"F": set(["C", "E"]),
57+
}
58+
59+
result = list(dfs_paths(graph, "A", "F")) # [['A', 'C', 'F'], ['A', 'B', 'E', 'F']]
5260
print(result)

find_m_to_last_llist.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@
55

66
from linked_list_data_structure import LinkedList
77

8+
89
def findMToLast(l_list, m):
9-
current = l_list.head
10-
count = 0
10+
current = l_list.head
11+
count = 0
12+
13+
while current is not None and count < m:
14+
count += 1
15+
current = current.getNextNode()
1116

12-
while current is not None and count < m:
13-
count+=1
14-
current = current.getNextNode()
17+
m_behind = l_list.head
18+
while current.next_node is not None:
19+
current = current.getNextNode()
20+
m_behind = m_behind.getNextNode()
1521

16-
m_behind = l_list.head
17-
while current.next_node is not None:
18-
current = current.getNextNode()
19-
m_behind = m_behind.getNextNode()
22+
return m_behind
2023

21-
return m_behind
2224

2325
linked_list = LinkedList()
2426
m_to_last = 3

find_pairs_sum_k.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# Given an array of numbers, find all the
22
# pairs of numbers which sum upto `k`
33

4+
45
def find_pairs(num_array, k):
5-
pairs_array = []
6-
for num in num_array:
7-
if (k-num) in num_array:
8-
pairs_array.append((num, (k-num)))
9-
return pairs_array
6+
pairs_array = []
7+
for num in num_array:
8+
if (k - num) in num_array:
9+
pairs_array.append((num, (k - num)))
10+
return pairs_array
11+
1012

1113
result = find_pairs([0, 14, 0, 4, 7, 8, 3, 5, 7], 11)
1214
print(result)

find_products_pair_k.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
def product_pair(arr, x):
2-
arr_sorted = sorted(arr)
2+
arr_sorted = sorted(arr)
33

4-
for i in range(0, len(arr_sorted)):
5-
sub_array = arr_sorted[i+1:]
6-
if x // arr_sorted[i] in sub_array:
7-
return True
4+
for i in range(0, len(arr_sorted)):
5+
sub_array = arr_sorted[i + 1 :]
6+
if x // arr_sorted[i] in sub_array:
7+
return True
88

9-
return False
9+
return False
1010

1111

12-
arr = [10, 20, 9, 40]
12+
arr = [10, 20, 9, 40]
1313
x = 400
1414

1515
res = product_pair(arr, x)

find_second_largest_in_binary_tree.py

+20-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# Given a binary tree, find the second largest
22
# node in it
33

4+
45
class Node:
5-
def __init__(self, data):
6-
self.data = data
7-
self.left = None
8-
self.right = None
6+
def __init__(self, data):
7+
self.data = data
8+
self.left = None
9+
self.right = None
10+
911

1012
def find_largest(root):
1113
current = root
@@ -14,21 +16,27 @@ def find_largest(root):
1416
return current.right.data
1517
current = current.right
1618

19+
1720
def find_second_largest(root):
1821
if root is None or (root.left is None and root.right is None):
1922
raise ValueError("Tree must atleast have 2 nodes")
20-
23+
2124
current = root
22-
25+
2326
while current is not None:
24-
if(current.left is not None and current.right is None):
27+
if current.left is not None and current.right is None:
2528
return find_largest(current.left)
26-
27-
if(current.right is not None and current.right.left is None and current.right.right is None):
29+
30+
if (
31+
current.right is not None
32+
and current.right.left is None
33+
and current.right.right is None
34+
):
2835
return current.data
29-
36+
3037
current = current.right
31-
38+
39+
3240
node = Node(10)
3341
node.left = Node(5)
3442
node.left.left = Node(1)
@@ -37,4 +45,4 @@ def find_second_largest(root):
3745
node.right.right = Node(100)
3846

3947
result = find_second_largest(node)
40-
print(result) # prints 50
48+
print(result) # prints 50

0 commit comments

Comments
 (0)