From d8a0afc55065467bc642fca181c6560565cb0247 Mon Sep 17 00:00:00 2001 From: Rafael Date: Fri, 7 Jul 2017 19:08:04 +0200 Subject: [PATCH 01/17] fix error in preshow --- data_structures/Binary Tree/binary_seach_tree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/Binary Tree/binary_seach_tree.py b/data_structures/Binary Tree/binary_seach_tree.py index 1dac948ae..7840b2a32 100644 --- a/data_structures/Binary Tree/binary_seach_tree.py +++ b/data_structures/Binary Tree/binary_seach_tree.py @@ -68,7 +68,7 @@ class BinarySearchTree: return False def preShow(self, curr_node): - if curr_node is None: + if curr_node is not None: print(curr_node.getLabel(), end=" ") self.preShow(curr_node.getLeft()) From 8384cbb8792cade306b630b56917e9ee41b4ba55 Mon Sep 17 00:00:00 2001 From: Rafael Date: Fri, 7 Jul 2017 20:06:33 +0200 Subject: [PATCH 02/17] finish AVL --- data_structures/AVL/AVL.py | 82 ++++++++++++++++++++++++++------------ 1 file changed, 56 insertions(+), 26 deletions(-) diff --git a/data_structures/AVL/AVL.py b/data_structures/AVL/AVL.py index 0e3008dc1..ad8458365 100644 --- a/data_structures/AVL/AVL.py +++ b/data_structures/AVL/AVL.py @@ -39,7 +39,7 @@ class Node: def setHeight(self, height): self.height = height - def getHeight(self, height): + def getHeight(self): return self.height @@ -72,38 +72,36 @@ class AVL: if node.getLabel() < dad_node.getLabel(): dad_node.setLeft(node) dad_node.setHeight(dad_node.getHeight() + 1) - - if (dad_node.getRight().getHeight() - - dad_node.getLeft.getHeight() > 1): - self.rebalance(dad_node) - else: dad_node.setRight(node) dad_node.setHeight(dad_node.getHeight() + 1) - - if (dad_node.getRight().getHeight() - - dad_node.getLeft.getHeight() > 1): - self.rebalance(dad_node) break def rebalance(self, node): - if (node.getRight().getHeight() - - node.getLeft.getHeight() > 1): - if (node.getRight().getHeight() > - node.getLeft.getHeight()): - pass + height_right = 0 + height_left = 0 + + if node.getRight() is not None: + height_right = node.getRight().getHeight() + + if node.getLeft() is not None: + height_left = node.getLeft().getHeight() + + if abs(height_left - height_right) > 1: + if height_left > height_right: + right_child = node.getRight() + if (right_child.getLeft().getHeight() > + right_child.getRight().getHeight()): + self.rotate_left(node) + else: + self.double_rotate_right(node) else: - pass - pass - elif (node.getRight().getHeight() - - node.getLeft.getHeight() > 2): - if (node.getRight().getHeight() > - node.getLeft.getHeight()): - pass - else: - pass - pass - pass + left_child = node.getLeft() + if (left_child.getLeft().getHeight() > + left_child.getRight().getHeight()): + self.double_rotate_left(node) + else: + self.rotate_right(node) def rotate_left(self, node): # TODO: is this pythonic enought? @@ -129,3 +127,35 @@ class AVL: def double_rotate_right(self, node): self.rotate_left(node.getLeft().getLeft()) self.rotate_right(node) + + def empty(self): + if self.root is None: + return True + return False + + def preShow(self, curr_node): + if curr_node is not None: + self.preShow(curr_node.getLeft()) + print(curr_node.getLabel(), end=" ") + self.preShow(curr_node.getRight()) + + def getRoot(self): + return self.root + +t = AVL() +t.insert(11) +t.insert(14) +t.insert(3) +t.insert(4) +t.insert(5) +t.insert(6) +t.insert(7) +t.insert(8) +t.insert(9) +t.insert(10) +t.insert(1) +t.insert(12) +t.insert(13) +t.insert(2) +t.insert(15) +t.preShow(t.getRoot()) From 67f23f59b15581be38c5d630018e590148928384 Mon Sep 17 00:00:00 2001 From: Rafael Date: Sat, 8 Jul 2017 01:26:49 +0200 Subject: [PATCH 03/17] rotate left almost working --- data_structures/AVL/AVL.py | 177 +++++++++++++++++++++++++++---------- 1 file changed, 129 insertions(+), 48 deletions(-) diff --git a/data_structures/AVL/AVL.py b/data_structures/AVL/AVL.py index ad8458365..4a360fee5 100644 --- a/data_structures/AVL/AVL.py +++ b/data_structures/AVL/AVL.py @@ -23,18 +23,24 @@ class Node: def setLeft(self, left): self.left = left - + self.left.setParent(self) + self.left.setHeight(self.height + 1) + def getRight(self): return self.rigt def setRight(self, right): self.rigt = right + self.rigt.setParent(self) + self.rigt.setHeight(self.height + 1) def getParent(self): return self.parent def setParent(self, parent): self.parent = parent + self.height = (self.parent.getHeight() + 1 if + (self.parent is not None) else 0) def setHeight(self, height): self.height = height @@ -53,6 +59,7 @@ class AVL: node = Node(value) if self.root is None: self.root = node + self.root.setHeight(0) self.size = 1 else: # Same as Binary Tree @@ -71,54 +78,59 @@ class AVL: else: if node.getLabel() < dad_node.getLabel(): dad_node.setLeft(node) - dad_node.setHeight(dad_node.getHeight() + 1) else: dad_node.setRight(node) - dad_node.setHeight(dad_node.getHeight() + 1) + self.rebalance(dad_node) break def rebalance(self, node): height_right = 0 height_left = 0 + n = node + while n is not None: + height_right = 0 + height_left = 0 - if node.getRight() is not None: - height_right = node.getRight().getHeight() + if node.getRight() is not None: + height_right = node.getRight().getHeight() - if node.getLeft() is not None: - height_left = node.getLeft().getHeight() + if node.getLeft() is not None: + height_left = node.getLeft().getHeight() - if abs(height_left - height_right) > 1: - if height_left > height_right: - right_child = node.getRight() - if (right_child.getLeft().getHeight() > - right_child.getRight().getHeight()): - self.rotate_left(node) + if abs(height_left - height_right) > 1: + if height_left > height_right: + left_child = node.getRight() + if (): + self.rotate_left(n) + break + else: + self.double_rotate_right(n) + break else: - self.double_rotate_right(node) - else: - left_child = node.getLeft() - if (left_child.getLeft().getHeight() > - left_child.getRight().getHeight()): - self.double_rotate_left(node) - else: - self.rotate_right(node) + right_child = node.getRight() + if right_child is not None: + h_right = (right_child.getRight().getHeight() + if (right_child.getRight() is not None) else 0) + h_left = (right_child.getLeft().getHeight() + if (right_child.getLeft() is not None) else 0) + + if (h_left > h_right): + self.double_rotate_left(n) + break + else: + self.rotate_right(n) + print(n.getLabel()) + break + n = n.getParent() def rotate_left(self, node): - # TODO: is this pythonic enought? - aux = node.getLabel() - node = aux.getRight() - node.setHeight(node.getHeight() - 1) - node.setLeft(Node(aux)) - node.getLeft().setHeight(node.getHeight() + 1) - node.getRight().setHeight(node.getRight().getHeight() - 1) + pass def rotate_right(self, node): - aux = node.getLabel() - node = aux.getLeft() - node.setHeight(node.getHeight() - 1) - node.setRight(Node(aux)) - node.getLeft().setHeight(node.getHeight() + 1) - node.getLeft().setHeight(node.getLeft().getHeight() - 1) + n = Node(node.getLabel()) + n.setRight(node.getRight()) + n.setLeft(Node(node.getParent().getLabel())) + node = n def double_rotate_left(self, node): self.rotate_right(node.getRight().getRight()) @@ -139,23 +151,92 @@ class AVL: print(curr_node.getLabel(), end=" ") self.preShow(curr_node.getRight()) + def preorden(self, curr_node): + if curr_node is not None: + self.preShow(curr_node.getLeft()) + self.preShow(curr_node.getRight()) + print(curr_node.getLabel(), end=" ") + def getRoot(self): return self.root t = AVL() -t.insert(11) -t.insert(14) -t.insert(3) -t.insert(4) -t.insert(5) -t.insert(6) -t.insert(7) -t.insert(8) -t.insert(9) -t.insert(10) +# t.insert(1) +# t.preShow(t.getRoot()) +# print("\n") +# t.preorden(t.getRoot()) +# print("\n") +# t.insert(2) +# t.preShow(t.getRoot()) +# print("\n") +# t.preorden(t.getRoot()) +# print("\n") +# t.insert(3) +# t.preShow(t.getRoot()) +# print("\n") +# t.preorden(t.getRoot()) +# print("\n") +# print(t.getRoot().getHeight()) +# print(t.getRoot().getRight().getHeight()) t.insert(1) -t.insert(12) -t.insert(13) t.insert(2) -t.insert(15) -t.preShow(t.getRoot()) +t.insert(3) +# t.insert(4) +# t.preShow(t.getRoot()) +# print("\n") +# t.preorden(t.getRoot()) +# print("\n") +# t.insert(5) +# t.preShow(t.getRoot()) +# print("\n") +# t.preorden(t.getRoot()) +# print("\n") +# t.insert(6) +# t.preShow(t.getRoot()) +# print("\n") +# t.preorden(t.getRoot()) +# print("\n") +# t.insert(7) +# t.preShow(t.getRoot()) +# print("\n") +# t.preorden(t.getRoot()) +# print("\n") +# t.insert(8) +# t.preShow(t.getRoot()) +# print("\n") +# t.preorden(t.getRoot()) +# print("\n") +# t.insert(9) +# t.preShow(t.getRoot()) +# print("\n") +# t.preorden(t.getRoot()) +# print("\n") +# t.insert(10) +# t.preShow(t.getRoot()) +# print("\n") +# t.preorden(t.getRoot()) +# print("\n") +# t.insert(11) +# t.preShow(t.getRoot()) +# print("\n") +# t.preorden(t.getRoot()) +# print("\n") +# t.insert(12) +# t.preShow(t.getRoot()) +# print("\n") +# t.preorden(t.getRoot()) +# print("\n") +# t.insert(13) +# t.preShow(t.getRoot()) +# print("\n") +# t.preorden(t.getRoot()) +# print("\n") +# t.insert(14) +# t.preShow(t.getRoot()) +# print("\n") +# t.preorden(t.getRoot()) +# print("\n") +# t.insert(15) +# t.preShow(t.getRoot()) +# print("\n") +# t.preorden(t.getRoot()) From d8cd33add2274af8f8ef6371f2bb019bab734b98 Mon Sep 17 00:00:00 2001 From: Rafael Date: Sat, 8 Jul 2017 20:50:47 +0200 Subject: [PATCH 04/17] using python properties --- data_structures/AVL/AVL.py | 67 +++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 38 deletions(-) diff --git a/data_structures/AVL/AVL.py b/data_structures/AVL/AVL.py index 4a360fee5..f1ac16ef8 100644 --- a/data_structures/AVL/AVL.py +++ b/data_structures/AVL/AVL.py @@ -12,41 +12,32 @@ class Node: self.parent = None self.height = 0 - def getLabel(self): - return self.label + @property + def right(self): + return self.right - def setLabel(self, label): - self.label = label + @right.setter + def right(self, node): + node.parent = self + self.right = node - def getLeft(self): - return self.left - - def setLeft(self, left): - self.left = left - self.left.setParent(self) - self.left.setHeight(self.height + 1) - - def getRight(self): - return self.rigt - - def setRight(self, right): - self.rigt = right - self.rigt.setParent(self) - self.rigt.setHeight(self.height + 1) - - def getParent(self): + @property + def left(self): return self.parent - def setParent(self, parent): - self.parent = parent - self.height = (self.parent.getHeight() + 1 if - (self.parent is not None) else 0) + @left.setter + def left(self, node): + node.parent = self + self.left = node - def setHeight(self, height): - self.height = height + @property + def parent(self): + return self.parent - def getHeight(self): - return self.height + @parent.setter + def parent(self, node): + self.parent = node + self.height = self.parent.height + 1 class AVL: @@ -59,7 +50,7 @@ class AVL: node = Node(value) if self.root is None: self.root = node - self.root.setHeight(0) + self.root.height = 0 self.size = 1 else: # Same as Binary Tree @@ -71,16 +62,18 @@ class AVL: dad_node = curr_node - if node.getLabel() < curr_node.getLabel(): - curr_node = curr_node.getLeft() + if node.label < curr_node.label: + curr_node = curr_node.left else: - curr_node = curr_node.getRight() + curr_node = curr_node.rigt else: - if node.getLabel() < dad_node.getLabel(): - dad_node.setLeft(node) + if node.label < dad_node.label: + dad_node.left = node else: - dad_node.setRight(node) + dad_node.right = node + self.rebalance(dad_node) + self.size += 1 break def rebalance(self, node): @@ -119,7 +112,6 @@ class AVL: break else: self.rotate_right(n) - print(n.getLabel()) break n = n.getParent() @@ -130,7 +122,6 @@ class AVL: n = Node(node.getLabel()) n.setRight(node.getRight()) n.setLeft(Node(node.getParent().getLabel())) - node = n def double_rotate_left(self, node): self.rotate_right(node.getRight().getRight()) From 8e41aca1b943d9bbbd742f57a0aa58cfa5e6c35e Mon Sep 17 00:00:00 2001 From: Rafael Date: Mon, 10 Jul 2017 18:29:45 +0200 Subject: [PATCH 05/17] node now eses pythonproperties --- data_structures/AVL/AVL.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/data_structures/AVL/AVL.py b/data_structures/AVL/AVL.py index f1ac16ef8..0c34cb597 100644 --- a/data_structures/AVL/AVL.py +++ b/data_structures/AVL/AVL.py @@ -7,9 +7,6 @@ class Node: def __init__(self, label): self.label = label - self.left = None - self.rigt = None - self.parent = None self.height = 0 @property @@ -23,7 +20,7 @@ class Node: @property def left(self): - return self.parent + return self.left @left.setter def left(self, node): @@ -48,6 +45,7 @@ class AVL: def insert(self, value): node = Node(value) + if self.root is None: self.root = node self.root.height = 0 @@ -65,7 +63,7 @@ class AVL: if node.label < curr_node.label: curr_node = curr_node.left else: - curr_node = curr_node.rigt + curr_node = curr_node.right else: if node.label < dad_node.label: dad_node.left = node @@ -92,7 +90,7 @@ class AVL: if abs(height_left - height_right) > 1: if height_left > height_right: - left_child = node.getRight() + # left_child = node.getRight() if (): self.rotate_left(n) break @@ -100,12 +98,12 @@ class AVL: self.double_rotate_right(n) break else: - right_child = node.getRight() + right_child = node.right if right_child is not None: - h_right = (right_child.getRight().getHeight() - if (right_child.getRight() is not None) else 0) - h_left = (right_child.getLeft().getHeight() - if (right_child.getLeft() is not None) else 0) + h_right = (right_child.right.height + if (right_child.right is not None) else 0) + h_left = (right_child.left.height + if (right_child.left is not None) else 0) if (h_left > h_right): self.double_rotate_left(n) @@ -119,9 +117,13 @@ class AVL: pass def rotate_right(self, node): - n = Node(node.getLabel()) - n.setRight(node.getRight()) - n.setLeft(Node(node.getParent().getLabel())) + aux = node.parent + node.parent = node + node.left = aux + + print(node.parent.label) + print(node.parent.right.label) + print(node.parent.left.label) def double_rotate_left(self, node): self.rotate_right(node.getRight().getRight()) From 6903d95b41206d757540f48230e245e7f28b8667 Mon Sep 17 00:00:00 2001 From: Rafael Leyva Ruiz Date: Mon, 2 Oct 2017 16:32:59 +0200 Subject: [PATCH 06/17] AVL done --- data_structures/AVL/AVL.py | 173 +++++++++++++------------------------ 1 file changed, 59 insertions(+), 114 deletions(-) diff --git a/data_structures/AVL/AVL.py b/data_structures/AVL/AVL.py index 0c34cb597..9717f1b6e 100644 --- a/data_structures/AVL/AVL.py +++ b/data_structures/AVL/AVL.py @@ -7,34 +7,42 @@ class Node: def __init__(self, label): self.label = label + self._parent = None + self._left = None + self._right = None self.height = 0 @property def right(self): - return self.right + return self._right @right.setter def right(self, node): - node.parent = self - self.right = node + if node is not None: + node._parent = self + self._right = node @property def left(self): - return self.left + return self._left @left.setter def left(self, node): - node.parent = self - self.left = node + if node is not None: + node._parent = self + self._left = node @property def parent(self): - return self.parent + return self._parent @parent.setter def parent(self, node): - self.parent = node - self.height = self.parent.height + 1 + if node is not None: + self._parent = node + self.height = self.parent.height + 1 + else: + self.height = 0 class AVL: @@ -45,7 +53,7 @@ class AVL: def insert(self, value): node = Node(value) - + if self.root is None: self.root = node self.root.height = 0 @@ -65,65 +73,72 @@ class AVL: else: curr_node = curr_node.right else: + node.height = dad_node.height + dad_node.height += 1 if node.label < dad_node.label: dad_node.left = node else: dad_node.right = node - - self.rebalance(dad_node) + self.rebalance(node) self.size += 1 break def rebalance(self, node): - height_right = 0 - height_left = 0 n = node + while n is not None: - height_right = 0 - height_left = 0 + height_right = n.height + height_left = n.height - if node.getRight() is not None: - height_right = node.getRight().getHeight() + if n.right is not None: + height_right = n.right.height - if node.getLeft() is not None: - height_left = node.getLeft().getHeight() + if n.left is not None: + height_left = n.left.height if abs(height_left - height_right) > 1: if height_left > height_right: - # left_child = node.getRight() - if (): + left_child = n.left + if left_child is not None: + h_right = (right_child.right.height + if (right_child.right is not None) else 0) + h_left = (right_child.left.height + if (right_child.left is not None) else 0) + if (h_left > h_right): self.rotate_left(n) break else: self.double_rotate_right(n) break else: - right_child = node.right + right_child = n.right if right_child is not None: h_right = (right_child.right.height if (right_child.right is not None) else 0) h_left = (right_child.left.height if (right_child.left is not None) else 0) - if (h_left > h_right): self.double_rotate_left(n) break else: self.rotate_right(n) break - n = n.getParent() + n = n.parent def rotate_left(self, node): - pass + aux = node.parent.label + node.parent.label = node.label + node.parent.right = Node(aux) + node.parent.right.height = node.parent.height + 1 + node.parent.left = node.right + def rotate_right(self, node): - aux = node.parent - node.parent = node - node.left = aux - - print(node.parent.label) - print(node.parent.right.label) - print(node.parent.left.label) + aux = node.parent.label + node.parent.label = node.label + node.parent.left = Node(aux) + node.parent.left.height = node.parent.height + 1 + node.parent.right = node.right def double_rotate_left(self, node): self.rotate_right(node.getRight().getRight()) @@ -140,96 +155,26 @@ class AVL: def preShow(self, curr_node): if curr_node is not None: - self.preShow(curr_node.getLeft()) - print(curr_node.getLabel(), end=" ") - self.preShow(curr_node.getRight()) + self.preShow(curr_node.left) + print(curr_node.label, end=" ") + self.preShow(curr_node.right) - def preorden(self, curr_node): + def preorder(self, curr_node): if curr_node is not None: - self.preShow(curr_node.getLeft()) - self.preShow(curr_node.getRight()) - print(curr_node.getLabel(), end=" ") + self.preShow(curr_node.left) + self.preShow(curr_node.right) + print(curr_node.label, end=" ") def getRoot(self): return self.root t = AVL() -# t.insert(1) -# t.preShow(t.getRoot()) -# print("\n") -# t.preorden(t.getRoot()) -# print("\n") -# t.insert(2) -# t.preShow(t.getRoot()) -# print("\n") -# t.preorden(t.getRoot()) -# print("\n") -# t.insert(3) -# t.preShow(t.getRoot()) -# print("\n") -# t.preorden(t.getRoot()) -# print("\n") -# print(t.getRoot().getHeight()) -# print(t.getRoot().getRight().getHeight()) t.insert(1) t.insert(2) t.insert(3) +# t.preShow(t.root) +# print("\n") # t.insert(4) -# t.preShow(t.getRoot()) -# print("\n") -# t.preorden(t.getRoot()) -# print("\n") # t.insert(5) -# t.preShow(t.getRoot()) -# print("\n") -# t.preorden(t.getRoot()) -# print("\n") -# t.insert(6) -# t.preShow(t.getRoot()) -# print("\n") -# t.preorden(t.getRoot()) -# print("\n") -# t.insert(7) -# t.preShow(t.getRoot()) -# print("\n") -# t.preorden(t.getRoot()) -# print("\n") -# t.insert(8) -# t.preShow(t.getRoot()) -# print("\n") -# t.preorden(t.getRoot()) -# print("\n") -# t.insert(9) -# t.preShow(t.getRoot()) -# print("\n") -# t.preorden(t.getRoot()) -# print("\n") -# t.insert(10) -# t.preShow(t.getRoot()) -# print("\n") -# t.preorden(t.getRoot()) -# print("\n") -# t.insert(11) -# t.preShow(t.getRoot()) -# print("\n") -# t.preorden(t.getRoot()) -# print("\n") -# t.insert(12) -# t.preShow(t.getRoot()) -# print("\n") -# t.preorden(t.getRoot()) -# print("\n") -# t.insert(13) -# t.preShow(t.getRoot()) -# print("\n") -# t.preorden(t.getRoot()) -# print("\n") -# t.insert(14) -# t.preShow(t.getRoot()) -# print("\n") -# t.preorden(t.getRoot()) -# print("\n") -# t.insert(15) -# t.preShow(t.getRoot()) -# print("\n") -# t.preorden(t.getRoot()) +# t.preShow(t.root) +# t.preorden(t.root) From 46b82fa249a0cb4811b3bfcbe99b68c32e33181f Mon Sep 17 00:00:00 2001 From: rajnishyadav321 Date: Thu, 5 Oct 2017 01:40:12 +0530 Subject: [PATCH 07/17] Added Next Greater Element Element NGE 4 --> 5 5 --> 25 2 --> 25 25 --> -1 --- data_structures/Stacks/next.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 data_structures/Stacks/next.py diff --git a/data_structures/Stacks/next.py b/data_structures/Stacks/next.py new file mode 100644 index 000000000..9765900c0 --- /dev/null +++ b/data_structures/Stacks/next.py @@ -0,0 +1,16 @@ +# Function to print element and NGE pair for all elements of list +def printNGE(arr): + + for i in range(0, len(arr), 1): + + next = -1 + for j in range(i+1, len(arr), 1): + if arr[i] < arr[j]: + next = arr[j] + break + + print(str(arr[i]) + " -- " + str(next)) + +# Driver program to test above function +arr = [11,13,21,3] +printNGE(arr) From 17e1a92f499bb3d4f8ccabe084934f76d048a92a Mon Sep 17 00:00:00 2001 From: Chris McLennon Date: Thu, 5 Oct 2017 22:24:02 -0500 Subject: [PATCH 08/17] Refactor Stack --- .../Stacks/Balanced_Parentheses.py | 27 -------- .../Stacks/Infix_To_Postfix_Conversion.py | 48 ------------- data_structures/Stacks/Stack.py | 50 -------------- .../Stacks/balanced_parentheses.py | 21 ++++++ .../Stacks/infix_to_postfix_conversion.py | 62 +++++++++++++++++ data_structures/Stacks/stack.py | 68 +++++++++++++++++++ 6 files changed, 151 insertions(+), 125 deletions(-) delete mode 100644 data_structures/Stacks/Balanced_Parentheses.py delete mode 100644 data_structures/Stacks/Infix_To_Postfix_Conversion.py delete mode 100644 data_structures/Stacks/Stack.py create mode 100644 data_structures/Stacks/balanced_parentheses.py create mode 100644 data_structures/Stacks/infix_to_postfix_conversion.py create mode 100644 data_structures/Stacks/stack.py diff --git a/data_structures/Stacks/Balanced_Parentheses.py b/data_structures/Stacks/Balanced_Parentheses.py deleted file mode 100644 index 6b7740380..000000000 --- a/data_structures/Stacks/Balanced_Parentheses.py +++ /dev/null @@ -1,27 +0,0 @@ -# Author: OMKAR PATHAK - -import Stack - -def parseParenthesis(string): - balanced = 1 - index = 0 - myStack = Stack.Stack(len(string)) - while (index < len(string)) and (balanced == 1): - check = string[index] - if check == '(': - myStack.push(check) - else: - if myStack.isEmpty(): - balanced = 0 - else: - myStack.pop() - index += 1 - - if balanced == 1 and myStack.isEmpty(): - return True - else: - return False - -if __name__ == '__main__': - print(parseParenthesis('((()))')) # True - print(parseParenthesis('((())')) # False diff --git a/data_structures/Stacks/Infix_To_Postfix_Conversion.py b/data_structures/Stacks/Infix_To_Postfix_Conversion.py deleted file mode 100644 index e33926a3d..000000000 --- a/data_structures/Stacks/Infix_To_Postfix_Conversion.py +++ /dev/null @@ -1,48 +0,0 @@ -# Author: OMKAR PATHAK - -import Stack - -def isOperand(char): - return (ord(char) >= ord('a') and ord(char) <= ord('z')) or (ord(char) >= ord('A') and ord(char) <= ord('Z')) - -def precedence(char): - if char == '+' or char == '-': - return 1 - elif char == '*' or char == '/': - return 2 - elif char == '^': - return 3 - else: - return -1 - -def infixToPostfix(myExp, myStack): - postFix = [] - for i in range(len(myExp)): - if (isOperand(myExp[i])): - postFix.append(myExp[i]) - elif(myExp[i] == '('): - myStack.push(myExp[i]) - elif(myExp[i] == ')'): - topOperator = myStack.pop() - while(not myStack.isEmpty() and topOperator != '('): - postFix.append(topOperator) - topOperator = myStack.pop() - else: - while (not myStack.isEmpty()) and (precedence(myExp[i]) <= precedence(myStack.peek())): - postFix.append(myStack.pop()) - myStack.push(myExp[i]) - - while(not myStack.isEmpty()): - postFix.append(myStack.pop()) - return ' '.join(postFix) - -if __name__ == '__main__': - myExp = 'a+b*(c^d-e)^(f+g*h)-i' - myExp = [i for i in myExp] - print('Infix:',' '.join(myExp)) - myStack = Stack.Stack(len(myExp)) - print('Postfix:',infixToPostfix(myExp, myStack)) - - # OUTPUT: - # Infix: a + b * ( c ^ d - e ) ^ ( f + g * h ) - i - # Postfix: a b c d ^ e - f g h * + ^ * + i - diff --git a/data_structures/Stacks/Stack.py b/data_structures/Stacks/Stack.py deleted file mode 100644 index 41bbdc9d2..000000000 --- a/data_structures/Stacks/Stack.py +++ /dev/null @@ -1,50 +0,0 @@ -# Author: OMKAR PATHAK - -class Stack(object): - def __init__(self, limit = 10): - self.stack = [] - self.limit = limit - - # for printing the stack contents - def __str__(self): - return ' '.join([str(i) for i in self.stack]) - - # for pushing an element on to the stack - def push(self, data): - if len(self.stack) >= self.limit: - print('Stack Overflow') - else: - self.stack.append(data) - - # for popping the uppermost element - def pop(self): - if len(self.stack) <= 0: - return -1 - else: - return self.stack.pop() - - # for peeking the top-most element of the stack - def peek(self): - if len(self.stack) <= 0: - return -1 - else: - return self.stack[len(self.stack) - 1] - - # to check if stack is empty - def isEmpty(self): - return self.stack == [] - - # for checking the size of stack - def size(self): - return len(self.stack) - -if __name__ == '__main__': - myStack = Stack() - for i in range(10): - myStack.push(i) - print(myStack) - myStack.pop() # popping the top element - print(myStack) - myStack.peek() # printing the top element - myStack.isEmpty() - myStack.size() diff --git a/data_structures/Stacks/balanced_parentheses.py b/data_structures/Stacks/balanced_parentheses.py new file mode 100644 index 000000000..1c9a84843 --- /dev/null +++ b/data_structures/Stacks/balanced_parentheses.py @@ -0,0 +1,21 @@ +from Stack import Stack + +__author__ = 'Omkar Pathak' + + +def balanced_parentheses(parentheses): + """ Use a stack to check if a string of parentheses are balanced.""" + stack = Stack(len(parentheses)) + for parenthesis in parentheses: + if parenthesis == '(': + stack.push(parenthesis) + elif parenthesis == ')': + stack.pop() + return not stack.is_empty() + + +if __name__ == '__main__': + examples = ['((()))', '((())'] + print('Balanced parentheses demonstration:\n') + for example in examples: + print(example + ': ' + str(balanced_parentheses(example))) diff --git a/data_structures/Stacks/infix_to_postfix_conversion.py b/data_structures/Stacks/infix_to_postfix_conversion.py new file mode 100644 index 000000000..f0a8fd072 --- /dev/null +++ b/data_structures/Stacks/infix_to_postfix_conversion.py @@ -0,0 +1,62 @@ +import string + +from Stack import Stack + +__author__ = 'Omkar Pathak' + + +def is_operand(char): + return char in string.ascii_letters or char in string.digits + + +def precedence(char): + """ Return integer value representing an operator's precedence, or + order of operation. + + https://en.wikipedia.org/wiki/Order_of_operations + """ + dictionary = {'+': 1, '-': 1, + '*': 2, '/': 2, + '^': 3} + return dictionary.get(char, -1) + + +def infix_to_postfix(expression): + """ Convert infix notation to postfix notation using the Shunting-yard + algorithm. + + https://en.wikipedia.org/wiki/Shunting-yard_algorithm + https://en.wikipedia.org/wiki/Infix_notation + https://en.wikipedia.org/wiki/Reverse_Polish_notation + """ + stack = Stack(len(expression)) + postfix = [] + for char in expression: + if is_operand(char): + postfix.append(char) + elif char not in {'(', ')'}: + while (not stack.is_empty() + and precedence(char) <= precedence(stack.peek())): + postfix.append(stack.pop()) + stack.push(char) + elif char == '(': + stack.push(char) + elif char == ')': + while not stack.is_empty() and stack.peek() != '(': + postfix.append(stack.pop()) + # Pop '(' from stack. If there is no '(', there is a mismatched + # parentheses. + if stack.peek() != '(': + raise ValueError('Mismatched parentheses') + stack.pop() + while not stack.is_empty(): + postfix.append(stack.pop()) + return ' '.join(postfix) + + +if __name__ == '__main__': + expression = 'a+b*(c^d-e)^(f+g*h)-i' + + print('Infix to Postfix Notation demonstration:\n') + print('Infix notation: ' + expression) + print('Postfix notation: ' + infix_to_postfix(expression)) diff --git a/data_structures/Stacks/stack.py b/data_structures/Stacks/stack.py new file mode 100644 index 000000000..0b100abf3 --- /dev/null +++ b/data_structures/Stacks/stack.py @@ -0,0 +1,68 @@ +__author__ = 'Omkar Pathak' + + +class Stack(object): + """ A stack is an abstract data type that serves as a collection of + elements with two principal operations: push() and pop(). push() adds an + element to the top of the stack, and pop() removes an element from the top + of a stack. The order in which elements come off of a stack are + Last In, First Out (LIFO). + + https://en.wikipedia.org/wiki/Stack_(abstract_data_type) + """ + + def __init__(self, limit=10): + self.stack = [] + self.limit = limit + + def __bool__(self): + return not bool(self.stack) + + def __str__(self): + return str(self.stack) + + def push(self, data): + """ Push an element to the top of the stack.""" + if len(self.stack) >= self.limit: + raise StackOverflowError + self.stack.append(data) + + def pop(self): + """ Pop an element off of the top of the stack.""" + if self.stack: + return self.stack.pop() + else: + raise IndexError('pop from an empty stack') + + def peek(self): + """ Peek at the top-most element of the stack.""" + if self.stack: + return self.stack[-1] + + def is_empty(self): + """ Check if a stack is empty.""" + return not bool(self.stack) + + def size(self): + """ Return the size of the stack.""" + return len(self.stack) + + +class StackOverflowError(BaseException): + pass + + +if __name__ == '__main__': + stack = Stack() + for i in range(10): + stack.push(i) + + print('Stack demonstration:\n') + print('Initial stack: ' + str(stack)) + print('pop(): ' + str(stack.pop())) + print('After pop(), the stack is now: ' + str(stack)) + print('peek(): ' + str(stack.peek())) + stack.push(100) + print('After push(100), the stack is now: ' + str(stack)) + print('is_empty(): ' + str(stack.is_empty())) + print('size(): ' + str(stack.size())) From e8d0c7e54a2ea80fd42f482a5a80ac0dd2bffa2b Mon Sep 17 00:00:00 2001 From: aravindiiitb Date: Fri, 6 Oct 2017 12:11:19 +0530 Subject: [PATCH 09/17] Added Code in Dynamic Programming section for Longest Strictly Increasing Subsequence in O(nlogn) time --- ...longest_increasing_subsequence_O(nlogn).py | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 dynamic_programming/longest_increasing_subsequence_O(nlogn).py diff --git a/dynamic_programming/longest_increasing_subsequence_O(nlogn).py b/dynamic_programming/longest_increasing_subsequence_O(nlogn).py new file mode 100644 index 000000000..3ebb4a137 --- /dev/null +++ b/dynamic_programming/longest_increasing_subsequence_O(nlogn).py @@ -0,0 +1,40 @@ +############################# +# Author: Aravind Kashyap +# File: lis.py +# comments: This programme outputs the Longest Strictly Increasing Subsequence in O(NLogN) +# Where N is the Number of elements in the list +############################# +def CeilIndex(v,l,r,key): + while r-l > 1: + m = (l + r)/2 + if v[m] >= key: + r = m + else: + l = m + + return r + + +def LongestIncreasingSubsequenceLength(v): + if(len(v) == 0): + return 0 + + tail = [0]*len(v) + length = 1 + + tail[0] = v[0] + + for i in range(1,len(v)): + if v[i] < tail[0]: + tail[0] = v[i] + elif v[i] > tail[length-1]: + tail[length] = v[i] + length += 1 + else: + tail[CeilIndex(tail,-1,length-1,v[i])] = v[i] + + return length + + +v = [2, 5, 3, 7, 11, 8, 10, 13, 6] +print LongestIncreasingSubsequenceLength(v) From b695175da3d6abac684a5385f5036046bb86abf8 Mon Sep 17 00:00:00 2001 From: Chinmoy Das Date: Sun, 8 Oct 2017 12:42:13 +0530 Subject: [PATCH 10/17] binary exponentiation --- other/binary_exponentiation.java | 77 ++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 other/binary_exponentiation.java diff --git a/other/binary_exponentiation.java b/other/binary_exponentiation.java new file mode 100644 index 000000000..56461fc81 --- /dev/null +++ b/other/binary_exponentiation.java @@ -0,0 +1,77 @@ +/** +* Binary Exponentiation +* This is a method to find a^b in a time complexity of O(log b) +* This is one of the most commonly used methods of finding powers. +* Also useful in cases where solution to (a^b)%c is required, +* where a,b,c can be numbers over the computers calculation limits. +*/ + +/** + * @author chinmoy159 + * @version 1.0 dated 10/08/2017 + */ +public class bin_expo +{ + /** + * function :- b_expo (int a, int b) + * returns a^b + */ + public static int b_expo(int a, int b) + { + /* + * iterative solution + */ + int res; + for (res = 1; b > 0; a *=a, b >>= 1) { + if ((b&1) == 1) { + res *= a; + } + } + return res; + /* + * recursive solution + if (b == 0) { + return 1; + } + if (b == 1) { + return a; + } + if ((b & 1) == 1) { + return a * b_expo(a*a, b >> 1); + } else { + return b_expo (a*a, b >> 1); + } + */ + } + /** + * function :- b_expo (long a, long b, long c) + * return (a^b)%c + */ + public static long b_expo(long a, long b, long c) + { + /* + * iterative solution + */ + long res; + for (res = 1l; b > 0; a *=a, b >>= 1) { + if ((b&1) == 1) { + res = ((res%c) * (a%c)) % c; + } + } + return res; + /* + * recursive solution + if (b == 0) { + return 1; + } + if (b == 1) { + return a; + } + if ((b & 1) == 1) { + return ((a%c) * (b_expo(a*a, b >> 1)%c))%c; + } else { + return b_expo (a*a, b >> 1)%c; + } + */ + } +} From 677dfe93bb25d5a4faed94ac90388c1bcd82228f Mon Sep 17 00:00:00 2001 From: Chinmoy Das Date: Sun, 8 Oct 2017 12:58:59 +0530 Subject: [PATCH 11/17] Delete binary_exponentiation.java --- other/binary_exponentiation.java | 77 -------------------------------- 1 file changed, 77 deletions(-) delete mode 100644 other/binary_exponentiation.java diff --git a/other/binary_exponentiation.java b/other/binary_exponentiation.java deleted file mode 100644 index 56461fc81..000000000 --- a/other/binary_exponentiation.java +++ /dev/null @@ -1,77 +0,0 @@ -/** -* Binary Exponentiation -* This is a method to find a^b in a time complexity of O(log b) -* This is one of the most commonly used methods of finding powers. -* Also useful in cases where solution to (a^b)%c is required, -* where a,b,c can be numbers over the computers calculation limits. -*/ - -/** - * @author chinmoy159 - * @version 1.0 dated 10/08/2017 - */ -public class bin_expo -{ - /** - * function :- b_expo (int a, int b) - * returns a^b - */ - public static int b_expo(int a, int b) - { - /* - * iterative solution - */ - int res; - for (res = 1; b > 0; a *=a, b >>= 1) { - if ((b&1) == 1) { - res *= a; - } - } - return res; - /* - * recursive solution - if (b == 0) { - return 1; - } - if (b == 1) { - return a; - } - if ((b & 1) == 1) { - return a * b_expo(a*a, b >> 1); - } else { - return b_expo (a*a, b >> 1); - } - */ - } - /** - * function :- b_expo (long a, long b, long c) - * return (a^b)%c - */ - public static long b_expo(long a, long b, long c) - { - /* - * iterative solution - */ - long res; - for (res = 1l; b > 0; a *=a, b >>= 1) { - if ((b&1) == 1) { - res = ((res%c) * (a%c)) % c; - } - } - return res; - /* - * recursive solution - if (b == 0) { - return 1; - } - if (b == 1) { - return a; - } - if ((b & 1) == 1) { - return ((a%c) * (b_expo(a*a, b >> 1)%c))%c; - } else { - return b_expo (a*a, b >> 1)%c; - } - */ - } -} From a36ca7c42f800ff24db287e72ab58f0230b4482c Mon Sep 17 00:00:00 2001 From: Chinmoy Das Date: Sun, 8 Oct 2017 12:59:18 +0530 Subject: [PATCH 12/17] Create binary_exponentiation.py --- other/binary_exponentiation.py | 49 ++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 other/binary_exponentiation.py diff --git a/other/binary_exponentiation.py b/other/binary_exponentiation.py new file mode 100644 index 000000000..57b218b55 --- /dev/null +++ b/other/binary_exponentiation.py @@ -0,0 +1,49 @@ +""" +* Binary Exponentiation +* This is a method to find a^b in a time complexity of O(log b) +* This is one of the most commonly used methods of finding powers. +* Also useful in cases where solution to (a^b)%c is required, +* where a,b,c can be numbers over the computers calculation limits. +* Done using iteration, can also be done using recursion + +* @author chinmoy159 +* @version 1.0 dated 10/08/2017 +""" + + +def b_expo(a, b): + res = 1 + while b > 0: + if b&1: + res *= a + + a *= a + b >>= 1 + + return res + + +def b_expo(a, b, c): + res = 1 + while b > 0: + if b&1: + res = ((res%c) * (a%c)) % c + + a *= a + b >>= 1 + + return res + +""" +* Wondering how this method works ! +* It's pretty simple. +* Let's say you need to calculate a ^ b +* RULE 1 : a ^ b = (a*a) ^ (b/2) ---- example : 4 ^ 4 = (4*4) ^ (4/2) = 16 ^ 2 +* RULE 2 : IF b is ODD, then ---- a ^ b = a * (a ^ (b - 1)) :: where (b - 1) is even. +* Once b is even, repeat the process to get a ^ b +* Repeat the process till b = 1 OR b = 0, because a^1 = a AND a^0 = 1 +* +* As far as the modulo is concerned, +* the fact : (a*b) % c = ((a%c) * (b%c)) % c +* Now apply RULE 1 OR 2 whichever is required. +""" From f4c6578ece43ef1ebcdfcab448c326f72a40763c Mon Sep 17 00:00:00 2001 From: Chinmoy Das Date: Sun, 8 Oct 2017 13:00:13 +0530 Subject: [PATCH 13/17] Binary Exponentiation for a^b --- other/binary_exponentiation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/binary_exponentiation.py b/other/binary_exponentiation.py index 57b218b55..964a646f8 100644 --- a/other/binary_exponentiation.py +++ b/other/binary_exponentiation.py @@ -1,5 +1,5 @@ """ -* Binary Exponentiation +* Binary Exponentiation for Powers * This is a method to find a^b in a time complexity of O(log b) * This is one of the most commonly used methods of finding powers. * Also useful in cases where solution to (a^b)%c is required, From f5917f589c6fe27a642cbf94ef6e230a40ab18f0 Mon Sep 17 00:00:00 2001 From: Chinmoy Das Date: Sun, 8 Oct 2017 13:10:05 +0530 Subject: [PATCH 14/17] Binary Exponentiation for Multiplication --- other/binary_exponentiation_2.py | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 other/binary_exponentiation_2.py diff --git a/other/binary_exponentiation_2.py b/other/binary_exponentiation_2.py new file mode 100644 index 000000000..217a616c9 --- /dev/null +++ b/other/binary_exponentiation_2.py @@ -0,0 +1,50 @@ +""" +* Binary Exponentiation with Multiplication +* This is a method to find a*b in a time complexity of O(log b) +* This is one of the most commonly used methods of finding result of multiplication. +* Also useful in cases where solution to (a*b)%c is required, +* where a,b,c can be numbers over the computers calculation limits. +* Done using iteration, can also be done using recursion + +* @author chinmoy159 +* @version 1.0 dated 10/08/2017 +""" + + +def b_expo(a, b): + res = 0 + while b > 0: + if b&1: + res += a + + a += a + b >>= 1 + + return res + + +def b_expo_mod(a, b, c): + res = 0 + while b > 0: + if b&1: + res = ((res%c) + (a%c)) % c + + a += a + b >>= 1 + + return res + + +""" +* Wondering how this method works ! +* It's pretty simple. +* Let's say you need to calculate a ^ b +* RULE 1 : a * b = (a+a) * (b/2) ---- example : 4 * 4 = (4+4) * (4/2) = 8 * 2 +* RULE 2 : IF b is ODD, then ---- a * b = a + (a * (b - 1)) :: where (b - 1) is even. +* Once b is even, repeat the process to get a * b +* Repeat the process till b = 1 OR b = 0, because a*1 = a AND a*0 = 0 +* +* As far as the modulo is concerned, +* the fact : (a+b) % c = ((a%c) + (b%c)) % c +* Now apply RULE 1 OR 2, whichever is required. +""" From 0393c5ad38d66d19cc7366b4bafd62cd984b049d Mon Sep 17 00:00:00 2001 From: Chinmoy Das Date: Sun, 8 Oct 2017 13:12:33 +0530 Subject: [PATCH 15/17] Update binary_exponentiation.py --- other/binary_exponentiation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/binary_exponentiation.py b/other/binary_exponentiation.py index 964a646f8..1a30fb8fd 100644 --- a/other/binary_exponentiation.py +++ b/other/binary_exponentiation.py @@ -23,7 +23,7 @@ def b_expo(a, b): return res -def b_expo(a, b, c): +def b_expo_mod(a, b, c): res = 1 while b > 0: if b&1: From 7447a9f9c7bdef4ceef3a93cee110525f683927d Mon Sep 17 00:00:00 2001 From: Uday Patel Date: Mon, 9 Oct 2017 03:19:39 +0100 Subject: [PATCH 16/17] Added Ternary Search Algorithm --- searches/ternary_search.py | 112 +++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 searches/ternary_search.py diff --git a/searches/ternary_search.py b/searches/ternary_search.py new file mode 100644 index 000000000..3b1c75314 --- /dev/null +++ b/searches/ternary_search.py @@ -0,0 +1,112 @@ +''' +This is a type of divide and conquer algorithm which divides the search space into +3 parts and finds the target value based on the property of the array or list +(usually monotonic property). + +Time Complexity : O(log3 N) +Space Complexity : O(1) +''' + +import sys + +# This is the precision for this function which can be altered. +# It is recommended for users to keep this number greater than or equal to 10. +precision = 10 + +# This is the linear search that will occur after the search space has become smaller. +def lin_search(left, right, A, target): + for i in range(left, right+1): + if(A[i] == target): + return i + +# This is the iterative method of the ternary search algorithm. +def ite_ternary_search(A, target): + left = 0 + right = len(A) - 1; + while(True): + if(left Date: Tue, 10 Oct 2017 21:46:07 -0700 Subject: [PATCH 17/17] Add Linear Congruential Generator --- other/LinearCongruentialGenerator.py | 34 ++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 other/LinearCongruentialGenerator.py diff --git a/other/LinearCongruentialGenerator.py b/other/LinearCongruentialGenerator.py new file mode 100644 index 000000000..b1eaa6119 --- /dev/null +++ b/other/LinearCongruentialGenerator.py @@ -0,0 +1,34 @@ +__author__ = "Tobias Carryer" + +from time import time + +class LinearCongruentialGenerator(object): + """ + A pseudorandom number generator. + """ + + def __init__( self, multiplier, increment, modulo, seed=int(time()) ): + """ + These parameters are saved and used when nextNumber() is called. + + modulo is the largest number that can be generated (exclusive). The most + efficent values are powers of 2. 2^32 is a common value. + """ + self.multiplier = multiplier + self.increment = increment + self.modulo = modulo + self.seed = seed + + def next_number( self ): + """ + The smallest number that can be generated is zero. + The largest number that can be generated is modulo-1. modulo is set in the constructor. + """ + self.seed = (self.multiplier * self.seed + self.increment) % self.modulo + return self.seed + +if __name__ == "__main__": + # Show the LCG in action. + lcg = LinearCongruentialGenerator(1664525, 1013904223, 2<<31) + while True : + print lcg.next_number() \ No newline at end of file