Skip to content

Commit 5729424

Browse files
committed
Merge branch 'maths_algorithm' of git://github.com/shivamarora1/Python into shivamarora1-maths_algorithm
2 parents 84ae001 + 768a39d commit 5729424

File tree

6 files changed

+166
-4
lines changed

6 files changed

+166
-4
lines changed

Maths/find_hcf.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Program to find the HCF of two Numbers
2+
def find_hcf(num_1, num_2):
3+
if num_1 == 0:
4+
return num_2
5+
if num_2 == 0:
6+
return num_1
7+
# Base Case
8+
if num_1 == num_2:
9+
return num_1
10+
if num_1 > num_2:
11+
return find_hcf(num_1 - num_2, num_2)
12+
return find_hcf(num_1, num_2 - num_1)
13+
14+
15+
def main():
16+
num_1 = 24
17+
num_2 = 34
18+
print('HCF of %s and %s is %s:' % (num_1, num_2, find_hcf(num_1, num_2)))
19+
20+
21+
if __name__ == '__main__':
22+
main()

Maths/find_lcm.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def find_lcm(num_1, num_2):
2+
max = num_1 if num_1 > num_2 else num_2
3+
while (True):
4+
if ((max % num_1 == 0) and (max % num_2 == 0)):
5+
break
6+
max += 1
7+
return max
8+
9+
10+
def main():
11+
num_1 = 12
12+
num_2 = 76
13+
print(find_lcm(num_1, num_2))
14+
15+
16+
if __name__ == '__main__':
17+
main()

binary_tree/basic_binary_tree.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Node:
2+
def __init__(self, data):
3+
self.data = data
4+
self.left = None
5+
self.right = None
6+
7+
8+
def depth_of_tree(tree):
9+
if tree is None:
10+
return 0
11+
else:
12+
depth_l_tree = depth_of_tree(tree.left)
13+
depth_r_tree = depth_of_tree(tree.right)
14+
if depth_l_tree > depth_r_tree:
15+
return 1 + depth_l_tree
16+
else:
17+
return 1 + depth_r_tree
18+
19+
20+
def is_full_binary_tree(tree):
21+
if tree is None:
22+
return True
23+
if (tree.left is None) and (tree.right is None):
24+
return True
25+
if (tree.left is not None) and (tree.right is not None):
26+
return (is_full_binary_tree(tree.left) and is_full_binary_tree(tree.right))
27+
else:
28+
return False
29+
30+
31+
def main():
32+
tree = Node(1)
33+
tree.left = Node(2)
34+
tree.right = Node(3)
35+
tree.left.left = Node(4)
36+
tree.left.right = Node(5)
37+
tree.left.right.left = Node(6)
38+
tree.right.left = Node(7)
39+
tree.right.left.left = Node(8)
40+
tree.right.left.left.right = Node(9)
41+
42+
print(is_full_binary_tree(tree))
43+
print(depth_of_tree(tree))
44+
45+
46+
if __name__ == '__main__':
47+
main()

graphs/basic_graphs.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from __future__ import print_function
22

33
try:
4-
raw_input # Python 2
4+
raw_input # Python 2
55
except NameError:
66
raw_input = input # Python 3
77

88
try:
9-
xrange # Python 2
9+
xrange # Python 2
1010
except NameError:
11-
xrange = range # Python 3
11+
xrange = range # Python 3
1212

1313
# Accept No. of Nodes and edges
1414
n, m = map(int, raw_input().split(" "))
@@ -141,7 +141,7 @@ def dijk(G, s):
141141

142142
def topo(G, ind=None, Q=[1]):
143143
if ind is None:
144-
ind = [0] * (len(G) + 1) # SInce oth Index is ignored
144+
ind = [0] * (len(G) + 1) # SInce oth Index is ignored
145145
for u in G:
146146
for v in G[u]:
147147
ind[v] += 1
@@ -279,3 +279,12 @@ def krusk(E_and_n):
279279
s[j].update(s[i])
280280
s.pop(i)
281281
break
282+
283+
284+
# find the isolated node in the graph
285+
def find_isolated_nodes(graph):
286+
isolated = []
287+
for node in graph:
288+
if not graph[node]:
289+
isolated.append(node)
290+
return isolated
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
def add(matrix_a, matrix_b):
2+
rows = len(matrix_a)
3+
columns = len(matrix_a[0])
4+
matrix_c = []
5+
for i in range(rows):
6+
list_1 = []
7+
for j in range(columns):
8+
val = matrix_a[i][j] + matrix_b[i][j]
9+
list_1.append(val)
10+
matrix_c.append(list_1)
11+
return matrix_c
12+
13+
14+
def multiply(matrix_a, matrix_b):
15+
matrix_c = []
16+
n = len(matrix_a)
17+
for i in range(n):
18+
list_1 = []
19+
for j in range(n):
20+
val = 0
21+
for k in range(n):
22+
val = val + matrix_a[i][k] * matrix_b[k][j]
23+
list_1.append(val)
24+
matrix_c.append(list_1)
25+
return matrix_c
26+
27+
28+
def main():
29+
matrix_a = [[12, 10], [3, 9]]
30+
matrix_b = [[3, 4], [7, 4]]
31+
print(add(matrix_a, matrix_b))
32+
print(multiply(matrix_a, matrix_b))
33+
34+
35+
if __name__ == '__main__':
36+
main()

other/palindrome.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Program to find whether given string is palindrome or not
2+
def is_palindrome(str):
3+
start_i = 0
4+
end_i = len(str) - 1
5+
while start_i < end_i:
6+
if str[start_i] == str[end_i]:
7+
start_i += 1
8+
end_i -= 1
9+
else:
10+
return False
11+
return True
12+
13+
14+
# Recursive method
15+
def recursive_palindrome(str):
16+
if len(str) <= 1:
17+
return True
18+
if str[0] == str[len(str) - 1]:
19+
return recursive_palindrome(str[1:-1])
20+
else:
21+
return False
22+
23+
24+
def main():
25+
str = 'ama'
26+
print(recursive_palindrome(str.lower()))
27+
print(is_palindrome(str.lower()))
28+
29+
30+
if __name__ == '__main__':
31+
main()

0 commit comments

Comments
 (0)