mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-19 21:19:47 +00:00
Merge branch 'maths_algorithm' of git://github.com/shivamarora1/Python into shivamarora1-maths_algorithm
This commit is contained in:
commit
5729424bdf
22
Maths/find_hcf.py
Normal file
22
Maths/find_hcf.py
Normal file
@ -0,0 +1,22 @@
|
||||
# Program to find the HCF of two Numbers
|
||||
def find_hcf(num_1, num_2):
|
||||
if num_1 == 0:
|
||||
return num_2
|
||||
if num_2 == 0:
|
||||
return num_1
|
||||
# Base Case
|
||||
if num_1 == num_2:
|
||||
return num_1
|
||||
if num_1 > num_2:
|
||||
return find_hcf(num_1 - num_2, num_2)
|
||||
return find_hcf(num_1, num_2 - num_1)
|
||||
|
||||
|
||||
def main():
|
||||
num_1 = 24
|
||||
num_2 = 34
|
||||
print('HCF of %s and %s is %s:' % (num_1, num_2, find_hcf(num_1, num_2)))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
17
Maths/find_lcm.py
Normal file
17
Maths/find_lcm.py
Normal file
@ -0,0 +1,17 @@
|
||||
def find_lcm(num_1, num_2):
|
||||
max = num_1 if num_1 > num_2 else num_2
|
||||
while (True):
|
||||
if ((max % num_1 == 0) and (max % num_2 == 0)):
|
||||
break
|
||||
max += 1
|
||||
return max
|
||||
|
||||
|
||||
def main():
|
||||
num_1 = 12
|
||||
num_2 = 76
|
||||
print(find_lcm(num_1, num_2))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
47
binary_tree/basic_binary_tree.py
Normal file
47
binary_tree/basic_binary_tree.py
Normal file
@ -0,0 +1,47 @@
|
||||
class Node:
|
||||
def __init__(self, data):
|
||||
self.data = data
|
||||
self.left = None
|
||||
self.right = None
|
||||
|
||||
|
||||
def depth_of_tree(tree):
|
||||
if tree is None:
|
||||
return 0
|
||||
else:
|
||||
depth_l_tree = depth_of_tree(tree.left)
|
||||
depth_r_tree = depth_of_tree(tree.right)
|
||||
if depth_l_tree > depth_r_tree:
|
||||
return 1 + depth_l_tree
|
||||
else:
|
||||
return 1 + depth_r_tree
|
||||
|
||||
|
||||
def is_full_binary_tree(tree):
|
||||
if tree is None:
|
||||
return True
|
||||
if (tree.left is None) and (tree.right is None):
|
||||
return True
|
||||
if (tree.left is not None) and (tree.right is not None):
|
||||
return (is_full_binary_tree(tree.left) and is_full_binary_tree(tree.right))
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
tree = Node(1)
|
||||
tree.left = Node(2)
|
||||
tree.right = Node(3)
|
||||
tree.left.left = Node(4)
|
||||
tree.left.right = Node(5)
|
||||
tree.left.right.left = Node(6)
|
||||
tree.right.left = Node(7)
|
||||
tree.right.left.left = Node(8)
|
||||
tree.right.left.left.right = Node(9)
|
||||
|
||||
print(is_full_binary_tree(tree))
|
||||
print(depth_of_tree(tree))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
@ -1,14 +1,14 @@
|
||||
from __future__ import print_function
|
||||
|
||||
try:
|
||||
raw_input # Python 2
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
try:
|
||||
xrange # Python 2
|
||||
xrange # Python 2
|
||||
except NameError:
|
||||
xrange = range # Python 3
|
||||
xrange = range # Python 3
|
||||
|
||||
# Accept No. of Nodes and edges
|
||||
n, m = map(int, raw_input().split(" "))
|
||||
@ -141,7 +141,7 @@ from collections import deque
|
||||
|
||||
def topo(G, ind=None, Q=[1]):
|
||||
if ind is None:
|
||||
ind = [0] * (len(G) + 1) # SInce oth Index is ignored
|
||||
ind = [0] * (len(G) + 1) # SInce oth Index is ignored
|
||||
for u in G:
|
||||
for v in G[u]:
|
||||
ind[v] += 1
|
||||
@ -279,3 +279,12 @@ def krusk(E_and_n):
|
||||
s[j].update(s[i])
|
||||
s.pop(i)
|
||||
break
|
||||
|
||||
|
||||
# find the isolated node in the graph
|
||||
def find_isolated_nodes(graph):
|
||||
isolated = []
|
||||
for node in graph:
|
||||
if not graph[node]:
|
||||
isolated.append(node)
|
||||
return isolated
|
||||
|
36
matrix/matrix_multiplication_addition.py
Normal file
36
matrix/matrix_multiplication_addition.py
Normal file
@ -0,0 +1,36 @@
|
||||
def add(matrix_a, matrix_b):
|
||||
rows = len(matrix_a)
|
||||
columns = len(matrix_a[0])
|
||||
matrix_c = []
|
||||
for i in range(rows):
|
||||
list_1 = []
|
||||
for j in range(columns):
|
||||
val = matrix_a[i][j] + matrix_b[i][j]
|
||||
list_1.append(val)
|
||||
matrix_c.append(list_1)
|
||||
return matrix_c
|
||||
|
||||
|
||||
def multiply(matrix_a, matrix_b):
|
||||
matrix_c = []
|
||||
n = len(matrix_a)
|
||||
for i in range(n):
|
||||
list_1 = []
|
||||
for j in range(n):
|
||||
val = 0
|
||||
for k in range(n):
|
||||
val = val + matrix_a[i][k] * matrix_b[k][j]
|
||||
list_1.append(val)
|
||||
matrix_c.append(list_1)
|
||||
return matrix_c
|
||||
|
||||
|
||||
def main():
|
||||
matrix_a = [[12, 10], [3, 9]]
|
||||
matrix_b = [[3, 4], [7, 4]]
|
||||
print(add(matrix_a, matrix_b))
|
||||
print(multiply(matrix_a, matrix_b))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
31
other/palindrome.py
Normal file
31
other/palindrome.py
Normal file
@ -0,0 +1,31 @@
|
||||
# Program to find whether given string is palindrome or not
|
||||
def is_palindrome(str):
|
||||
start_i = 0
|
||||
end_i = len(str) - 1
|
||||
while start_i < end_i:
|
||||
if str[start_i] == str[end_i]:
|
||||
start_i += 1
|
||||
end_i -= 1
|
||||
else:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
# Recursive method
|
||||
def recursive_palindrome(str):
|
||||
if len(str) <= 1:
|
||||
return True
|
||||
if str[0] == str[len(str) - 1]:
|
||||
return recursive_palindrome(str[1:-1])
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
str = 'ama'
|
||||
print(recursive_palindrome(str.lower()))
|
||||
print(is_palindrome(str.lower()))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
x
Reference in New Issue
Block a user