From 506172279a15d4fac19b11a38e31ff9c21d24d44 Mon Sep 17 00:00:00 2001 From: Shivam Arora Date: Wed, 31 Oct 2018 15:17:11 +0530 Subject: [PATCH 1/4] Addition and multiplication algorithm of two square matrix --- matrix/matrix_multiplication_addition.py | 36 ++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 matrix/matrix_multiplication_addition.py diff --git a/matrix/matrix_multiplication_addition.py b/matrix/matrix_multiplication_addition.py new file mode 100644 index 000000000..c387c43d4 --- /dev/null +++ b/matrix/matrix_multiplication_addition.py @@ -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() From 1a5df6bc466c05be4a271e93e8bf4687ab9e62e6 Mon Sep 17 00:00:00 2001 From: Shivam Arora Date: Wed, 31 Oct 2018 16:14:30 +0530 Subject: [PATCH 2/4] Added the method to find the isolated nodes in the graph --- graphs/basic_graphs.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/graphs/basic_graphs.py b/graphs/basic_graphs.py index c9a269f1a..3b3abeb17 100644 --- a/graphs/basic_graphs.py +++ b/graphs/basic_graphs.py @@ -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 From 11d0d641adf32a7e976bf9df8c4dc9ba19bba3b4 Mon Sep 17 00:00:00 2001 From: Shivam Arora Date: Wed, 31 Oct 2018 17:28:49 +0530 Subject: [PATCH 3/4] Binary graph algorithms to find height of binary tree and to check whether the given binary tree is full binary or not --- binary_tree/basic_binary_tree.py | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 binary_tree/basic_binary_tree.py diff --git a/binary_tree/basic_binary_tree.py b/binary_tree/basic_binary_tree.py new file mode 100644 index 000000000..6cdeb1a69 --- /dev/null +++ b/binary_tree/basic_binary_tree.py @@ -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() From 768a39d8322730cf8f89a84b9d2d1086118fc8cc Mon Sep 17 00:00:00 2001 From: Shivam Arora Date: Fri, 23 Nov 2018 22:21:07 +0530 Subject: [PATCH 4/4] Program for finding the HCF,LCM and Palindrome using and recursion and non recursion --- Maths/find_hcf.py | 22 ++++++++++++++++++++++ Maths/find_lcm.py | 17 +++++++++++++++++ other/palindrome.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 Maths/find_hcf.py create mode 100644 Maths/find_lcm.py create mode 100644 other/palindrome.py diff --git a/Maths/find_hcf.py b/Maths/find_hcf.py new file mode 100644 index 000000000..e4315d8d3 --- /dev/null +++ b/Maths/find_hcf.py @@ -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() diff --git a/Maths/find_lcm.py b/Maths/find_lcm.py new file mode 100644 index 000000000..58beb3e37 --- /dev/null +++ b/Maths/find_lcm.py @@ -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() diff --git a/other/palindrome.py b/other/palindrome.py new file mode 100644 index 000000000..990ec844f --- /dev/null +++ b/other/palindrome.py @@ -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()