Skip to content

Commit 0534a57

Browse files
authored
Data-structures
0 parents  commit 0534a57

File tree

60 files changed

+2189
-0
lines changed

Some content is hidden

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

60 files changed

+2189
-0
lines changed

Diff for: data-structures/2d-array.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/python3
2+
3+
import sys
4+
5+
6+
arr = []
7+
for arr_i in range(6):
8+
arr_t = [int(arr_temp) for arr_temp in input().strip().split(' ')]
9+
arr.append(arr_t)
10+
11+
12+
best = 0
13+
for ind_i in range(6-2):
14+
for ind_j in range(6-2):
15+
temp = 0
16+
temp += arr[ind_i][ind_j] + arr[ind_i][ind_j+1] + arr[ind_i][ind_j+2]
17+
temp += arr[ind_i+1][ind_j+1]
18+
temp += arr[ind_i+2][ind_j] + arr[ind_i+2][ind_j+1] + arr[ind_i+2][ind_j+2]
19+
if ind_i == 0 and ind_j == 0:
20+
best = temp
21+
else:
22+
best = max(best, temp)
23+
24+
print(best)

Diff for: data-structures/and-xor-or.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python3
2+
3+
def solution(arr):
4+
res = 0
5+
stack = []
6+
7+
for el in arr:
8+
while stack:
9+
res = max(res, el^stack[-1])
10+
11+
if el < stack[-1]:
12+
stack.pop()
13+
else:
14+
break
15+
stack.append(el)
16+
17+
return res
18+
19+
if __name__ == "__main__":
20+
n = int(input().strip())
21+
arr = [int(x) for x in input().strip().split()]
22+
print(solution(arr))

Diff for: data-structures/array-left-rotation.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/python3
2+
3+
import sys
4+
5+
def leftRotation(a, d):
6+
out = list(a)
7+
a_len = len(a)
8+
for ind, el in enumerate(a):
9+
out[(ind + a_len - d) % a_len] = el
10+
11+
return out
12+
13+
if __name__ == "__main__":
14+
n, d = input().strip().split(' ')
15+
n, d = [int(n), int(d)]
16+
a = list(map(int, input().strip().split(' ')))
17+
result = leftRotation(a, d)
18+
print (" ".join(map(str, result)))
19+
20+

Diff for: data-structures/arrays-ds.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/python3
2+
3+
import sys
4+
5+
6+
n = int(input().strip())
7+
arr = [int(arr_temp) for arr_temp in input().strip().split(' ')]
8+
9+
print(" ".join(map(str, arr[::-1])))

Diff for: data-structures/balanced-brackets.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/python3
2+
3+
import sys
4+
5+
def isBalanced(s):
6+
stack = []
7+
8+
for letter in s:
9+
if letter == '{':
10+
stack.append(1)
11+
elif letter == '[':
12+
stack.append(2)
13+
elif letter == '(':
14+
stack.append(3)
15+
elif letter == '}':
16+
if len(stack) == 0:
17+
return False
18+
if stack.pop() != 1:
19+
return False
20+
elif letter == ']':
21+
if len(stack) == 0:
22+
return False
23+
if stack.pop() != 2:
24+
return False
25+
elif letter == ')':
26+
if len(stack) == 0:
27+
return False
28+
if stack.pop() != 3:
29+
return False
30+
31+
return len(stack) == 0
32+
33+
if __name__ == "__main__":
34+
t = int(input().strip())
35+
for a0 in range(t):
36+
s = input().strip()
37+
result = isBalanced(s)
38+
if result is True:
39+
print('YES')
40+
else:
41+
print('NO')

Diff for: data-structures/binary-search-tree-insertion.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Node is defined as
3+
self.left (the left child of the node)
4+
self.right (the right child of the node)
5+
self.data (the value of the node)"""
6+
7+
def insert(root, val):
8+
if root is None:
9+
root = Node(data=val)
10+
return root
11+
12+
current = root
13+
while True:
14+
if current.data > val:
15+
if current.left is not None:
16+
current = current.left
17+
else:
18+
current.left = Node(data=val)
19+
break
20+
else:
21+
if current.right is not None:
22+
current = current.right
23+
else:
24+
current.right = Node(data=val)
25+
break
26+
27+
return root
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
Node is defined as
3+
self.left (the left child of the node)
4+
self.right (the right child of the node)
5+
self.data (the value of the node)
6+
"""
7+
8+
def lca(root , v1 , v2):
9+
vals = sorted([v1, v2])
10+
v1, v2 = vals[0], vals[1]
11+
node = root
12+
while True:
13+
if v1 < v2 < node.data:
14+
node = node.left
15+
if v1 > v2 > node.data:
16+
node = node.right
17+
if v1 <= node.data <= v2:
18+
break
19+
return node
20+

Diff for: data-structures/castle-on-the-grid.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
from collections import deque
5+
6+
def is_safe(grid, x, y, distances):
7+
return x >= 0 and x < len(grid) and y >= 0 and y < len(grid) and distances[x][y] == -1 and grid[x][y] != 'X'
8+
9+
def get_safe_moves(grid, node, distances):
10+
directions = [(-1, 0), (0, 1), (1, 0), (0, -1)]
11+
variants = []
12+
13+
for di in directions:
14+
nunode = (node[0] + di[0], node[1] + di[1])
15+
while is_safe(grid, nunode[0], nunode[1], distances):
16+
variants.append(nunode)
17+
nunode = (nunode[0] + di[0], nunode[1] + di[1])
18+
19+
return variants
20+
21+
22+
def minimumMoves(grid, startX, startY, goalX, goalY):
23+
next_to_visit = deque()
24+
node = (startX, startY)
25+
next_to_visit.appendleft(node)
26+
distances = [[-1]*len(grid) for _ in range(len(grid))]
27+
distances[startX][startY] = 0
28+
29+
while next_to_visit:
30+
node = next_to_visit.pop()
31+
#print("point = ({}, {})".format(node[0], node[1]))
32+
#for row in distances:
33+
# print(row)
34+
#print()
35+
height = distances[node[0]][node[1]]
36+
37+
variants = get_safe_moves(grid, node, distances)
38+
39+
for var in variants:
40+
if var == (goalX, goalY):
41+
return height + 1
42+
distances[var[0]][var[1]] = height + 1
43+
next_to_visit.appendleft(var)
44+
45+
return -1
46+
47+
48+
if __name__ == "__main__":
49+
n = int(input().strip())
50+
grid = []
51+
for _ in range(n):
52+
layer = list(input().strip())
53+
grid.append(layer)
54+
startX, startY, goalX, goalY = [int(i) for i in input().strip().split()]
55+
result = minimumMoves(grid, startX, startY, goalX, goalY)
56+
print(result)

Diff for: data-structures/compare-two-linked-lists.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
Merge two linked list
3+
head could be None as well for empty list
4+
Node is defined as
5+
6+
class Node(object):
7+
8+
def __init__(self, data=None, next_node=None):
9+
self.data = data
10+
self.next = next_node
11+
12+
return back the head of the linked list in the below method.
13+
"""
14+
15+
def CompareLists(headA, headB):
16+
if headA is None and headB is None:
17+
return 0
18+
else:
19+
nodeA = headA
20+
nodeB = headB
21+
while nodeA and nodeB:
22+
if nodeA.data != nodeB.data:
23+
return 0
24+
nodeA = nodeA.next
25+
nodeB = nodeB.next
26+
27+
if nodeA is None and nodeB is None:
28+
return 1
29+
else:
30+
return 0
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+

Diff for: data-structures/components-in-graph.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
5+
class disjoint_set:
6+
class Node:
7+
def __init__(self, data = 0):
8+
self.data = data
9+
self.parent = self
10+
self.rank = 0
11+
12+
def __init__(self):
13+
self.items = dict()
14+
15+
def make_set(self, data):
16+
if not data in self.items:
17+
self.items[data] = self.Node(data)
18+
return self.items
19+
20+
def find_set(self, data):
21+
if data in self.items:
22+
node = self.items[data]
23+
else:
24+
return False
25+
26+
if node.parent == node:
27+
return node
28+
node.parent = self.find_set(node.parent.data)
29+
30+
return node.parent
31+
32+
def union(self, rep1, rep2):
33+
node1 = self.find_set(rep1)
34+
node2 = self.find_set(rep2)
35+
36+
#print("union: node1 = {} node2 = {}".format(node1.data, node2.data))
37+
38+
if node1 and node2 and node1 != node2:
39+
if node1.rank >= node2.rank:
40+
if node1.rank == node2.rank:
41+
node1.rank += 1
42+
node2.parent = node1
43+
else:
44+
node1.parent = node2
45+
46+
return True
47+
48+
if __name__ == "__main__":
49+
n = int(input().strip())
50+
output = [0] * (n+1)
51+
52+
dset = disjoint_set()
53+
54+
for _ in range(n):
55+
rep1, rep2 = [int(x) for x in input().strip().split()]
56+
57+
dset.make_set(rep1)
58+
dset.make_set(rep2)
59+
60+
dset.union(rep1, rep2)
61+
62+
for el in dset.items.keys():
63+
output[dset.find_set(el).data] += 1
64+
65+
dset_max = max(output)
66+
dset_min = min(filter(lambda x: x > 0, output))
67+
68+
print("{} {}".format(dset_min, dset_max))
69+

Diff for: data-structures/contacts.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env python3
2+
3+
class Trie:
4+
def __init__(self):
5+
self.count = 1
6+
self.children = {}
7+
8+
def add(self, name):
9+
node = self
10+
for let in name:
11+
if let in node.children.keys():
12+
node.children[let].count += 1
13+
node = node.children[let]
14+
else:
15+
node.children[let] = Trie()
16+
node = node.children[let]
17+
18+
def find(self, name):
19+
node = self
20+
for let in name:
21+
if let in node.children.keys():
22+
res = node.children[let].count
23+
node = node.children[let]
24+
else:
25+
res = 0
26+
break
27+
28+
return res
29+
30+
def print(self):
31+
print("count = {} keys = {}".format(self.count, self.children.keys()))
32+
for key in self.children.keys():
33+
self.children[key].print()
34+
35+
if __name__ == "__main__":
36+
t = int(input().strip())
37+
trie = Trie()
38+
39+
for _ in range(t):
40+
args = input().strip().split()
41+
42+
if args[0] == 'add':
43+
trie.add(args[1])
44+
elif args[0] == 'find':
45+
print(trie.find(args[1]))
46+

0 commit comments

Comments
 (0)