diff --git a/README.md b/README.md index 5c8de70..3461466 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,12 @@ -Python and Algorithms & Data Structures -======================================= +# Python and Algorithms & Data Structures This repository contains a comprehensive study on Algorithms & Data Structures in Python, including an ["e-book" I wrote](http://mariwahl.us/docs/algorithms_in_python.pdf). ![](http://i.imgur.com/XruamX1.png) - -Source Code Structure ---------------------- +--- +## Source Code Structure src/ @@ -57,10 +55,12 @@ src/ ├── sorting +└── Extra Interview Problems -Installation ------------- +---- +## Installation + The snippets are designed to be used individually. However, If you want to install all fo the libraries in your [virtualenv](https://coderwall.com/p/8-aeka), do this: ``` @@ -68,22 +68,28 @@ $ pip install -r requirements.txt ``` +---- +## Further Learning -Further Learning ------------------ -[Check out my lessons in machine learning.] (https://github.com/mariwahl/Machine-Learning-Lessons) +### My Work: + +* [Check out my lessons in machine learning.] (https://github.com/mariwahl/Machine-Learning-Lessons) -[Check out my lessons in numerical methods.](https://github.com/mariwahl/Numerical-Methods-for-Physics) +* [Check out my lessons in numerical methods.](https://github.com/mariwahl/Numerical-Methods-for-Physics) -[Neat Problems in Python and Flask](https://github.com/mariwahl/Neat-Problems-in-Python-and-Flask) +* [Neat Problems in Python and Flask](https://github.com/mariwahl/Neat-Problems-in-Python-and-Flask) + +### Third Party: + +* [Interactive Python](http://interactivepython.org/) +---- +## License -License ----------- When making a reference to my work, please use my twitter handle [_b_t_3_](https://twitter.com/_b_t_3_) or my [website](http://bt3gl.github.io/index.html). Creative Commons License
diff --git a/src/extra_interview_problems/glassdoor/2014/abstract_strucutres/HashTable.py b/src/extra_interview_problems/abstract_strucutres/HashTable.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/abstract_strucutres/HashTable.py rename to src/extra_interview_problems/abstract_strucutres/HashTable.py diff --git a/src/extra_interview_problems/glassdoor/2014/abstract_strucutres/queue_with_stack.py b/src/extra_interview_problems/abstract_strucutres/queue_with_stack.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/abstract_strucutres/queue_with_stack.py rename to src/extra_interview_problems/abstract_strucutres/queue_with_stack.py diff --git a/src/extra_interview_problems/glassdoor/2014/abstract_strucutres/stack_with_minumum.py b/src/extra_interview_problems/abstract_strucutres/stack_with_minumum.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/abstract_strucutres/stack_with_minumum.py rename to src/extra_interview_problems/abstract_strucutres/stack_with_minumum.py diff --git a/src/extra_interview_problems/glassdoor/2014/advanced/lru_cache.py b/src/extra_interview_problems/advanced/lru_cache.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/advanced/lru_cache.py rename to src/extra_interview_problems/advanced/lru_cache.py diff --git a/src/extra_interview_problems/glassdoor/2014/bitwise/bitwise.txt b/src/extra_interview_problems/bitwise/bitwise.txt similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/bitwise/bitwise.txt rename to src/extra_interview_problems/bitwise/bitwise.txt diff --git a/src/extra_interview_problems/glassdoor/2014/bitwise/clear_bits.py b/src/extra_interview_problems/bitwise/clear_bits.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/bitwise/clear_bits.py rename to src/extra_interview_problems/bitwise/clear_bits.py diff --git a/src/extra_interview_problems/glassdoor/2014/bitwise/find_bit_len.py b/src/extra_interview_problems/bitwise/find_bit_len.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/bitwise/find_bit_len.py rename to src/extra_interview_problems/bitwise/find_bit_len.py diff --git a/src/extra_interview_problems/glassdoor/2014/bitwise/get_bit.py b/src/extra_interview_problems/bitwise/get_bit.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/bitwise/get_bit.py rename to src/extra_interview_problems/bitwise/get_bit.py diff --git a/src/extra_interview_problems/glassdoor/2014/bitwise/num_bits_to_convert_2_nums.py b/src/extra_interview_problems/bitwise/num_bits_to_convert_2_nums.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/bitwise/num_bits_to_convert_2_nums.py rename to src/extra_interview_problems/bitwise/num_bits_to_convert_2_nums.py diff --git a/src/extra_interview_problems/glassdoor/2014/bitwise/set_bit.py b/src/extra_interview_problems/bitwise/set_bit.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/bitwise/set_bit.py rename to src/extra_interview_problems/bitwise/set_bit.py diff --git a/src/extra_interview_problems/glassdoor/2014/bitwise/swap_in_place.py b/src/extra_interview_problems/bitwise/swap_in_place.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/bitwise/swap_in_place.py rename to src/extra_interview_problems/bitwise/swap_in_place.py diff --git a/src/extra_interview_problems/glassdoor/2014/bitwise/update_bit.py b/src/extra_interview_problems/bitwise/update_bit.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/bitwise/update_bit.py rename to src/extra_interview_problems/bitwise/update_bit.py diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/max_subarray_stocks.py b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/max_subarray_stocks.py deleted file mode 100755 index 61d2957..0000000 --- a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/max_subarray_stocks.py +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env python - -__author__ = "bt3" - - - -def beating_stock(array): - - imin = 0 - i = 1 - deal = [array[i] - array[imin], imin, i] - - while i < len(array): - - deal_here = array[i] - array[imin] - if deal_here > deal[0]: - deal = [deal_here, imin, i] - - elif array[i] < array[imin]: - imin = i - - i += 1 - - return deal[0], array[deal[1]], array[deal[2]] - - - -if __name__ == '__main__': - array = [7, 2, 3, 6, 5, 8, 5, 3, 4] - print(array) - print("Profit: %d, buying at %d, selling at %d." %(beating_stock(array))) \ No newline at end of file diff --git a/src/extra_interview_problems/glassdoor/2014/trees/binary_search_tree.py b/src/extra_interview_problems/glassdoor/2014/trees/binary_search_tree.py deleted file mode 100755 index f3da045..0000000 --- a/src/extra_interview_problems/glassdoor/2014/trees/binary_search_tree.py +++ /dev/null @@ -1,72 +0,0 @@ -#!/usr/bin/python - -__author__ = "bt3" - - -from binary_tree import NodeBT, BinaryTree - - -class NodeBST(NodeBT): - - def __init__(self, item=None, level=0): - self.item = item - self.level = level - self.left = None - self.right = None - - - def _addNextNode(self, value, level_here=1): - new_node = NodeBST(value, level_here) - if not self.item: - self.item = new_node - else: - if value > self.item: - self.right = self.right and self.right._addNextNode(value, level_here+1) or new_node - elif value < self.item: - self.left = self.left and self.left._addNextNode(value, level_here+1) or new_node - else: - print("BSTs do not support repeated items.") - return self # this is necessary!!! - - - - def _searchForNode(self, value): - if self.item == value: - return self - elif self.left and value < self.item: - return self.left._searchForNode(value) - elif self.right and value > self.item: - return self.right._searchForNode(value) - else: - return False - - - -class BinarySearchTree(BinaryTree): - - def __init__(self): - self.root = None - - def addNode(self, value): - if not self.root: - self.root = NodeBST(value) - else: - self.root._addNextNode(value) - - - - -if __name__ == '__main__': - bst = BinarySearchTree() - print "Adding nodes 1 to 10 in the tree..." - for i in range(1, 10): - bst.addNode(i) - print "Is 8 a leaf? ", bst.isLeaf(8) - print "Whats the level of node 8? ", bst.getNodeLevel(8) - print "Is node 10 a root? ", bst.isRoot(10) - print "Is node 1 a root? ", bst.isRoot(1) - print "Whats the tree height? ", bst.getHeight() - print "Is this tree BST? ", bst.isBST() - print "Is this tree balanced? ", bst.isBalanced() - - diff --git a/src/extra_interview_problems/glassdoor/2014/trees/binary_tree.py b/src/extra_interview_problems/glassdoor/2014/trees/binary_tree.py deleted file mode 100755 index 282a28a..0000000 --- a/src/extra_interview_problems/glassdoor/2014/trees/binary_tree.py +++ /dev/null @@ -1,205 +0,0 @@ -#!/usr/bin/python - -__author__ = "bt3" - - - -''' Implementation of a binary tree and its properties. For example, the following bt: - - 1 ---> level 0 - 2 3 ---> level 1 - 4 5 ---> level 2 - 6 7 ---> level 3 - 8 9 ---> level 4 - - has the following properties: - - - SIZE OR NUMBER OF NODES: n = 9 - - NUMBER OF BRANCHES OR INTERNAL NODES: b = n-1 = 8 - - VALUE OF ROOT = 1 - - MAX_DEPTH OR HEIGHT: h = 4 - - IS BALANCED? NO - - IS BST? NO -''' - - -class NodeBT(object): - def __init__(self, item=None, level=0): - self.item = item - self.level = level - self.left = None - self.right = None - - - def __repr__(self): - return '{}'.format(self.item) - - - def _addNextNode(self, value, level_here=1): - new_node = NodeBT(value, level_here) - if not self.item: - self.item = new_node - elif not self.left: - self.left = new_node - elif not self.right: - self.right = new_node - else: - self.left = self.left._addNextNode(value, level_here+1) - return self ## this is important, because the node return to the main - - - def _searchForNode(self, value): - if self.item == value: - return self - else: - found = None - if self.left: - found = self.left._searchForNode(value) - if self.right: - found = found or self.right._searchForNode(value) - return found - - - def _isLeaf(self): - return not self.right and not self.left - - - def _getMaxHeight(self): - ''' Get the max height at the node, O(n)''' - levelr, levell = 0, 0 - if self.right: - levelr = self.right._getMaxHeight() + 1 - if self.left: - levell = self.left._getMaxHeight() + 1 - return max(levelr, levell) - - - def _getMinHeight(self, level=0): - ''' Get the min height at the node, O(n)''' - levelr, levell = -1, -1 - if self.right: - levelr = self.right._getMinHeight(level +1) - if self.left: - levell = self.left._getMinHeight(level +1) - return min(levelr, levell) + 1 - - - def _isBalanced(self): - ''' Find whether the tree is balanced, by calculating heights first, O(n2) ''' - if self._getMaxHeight() - self._getMinHeight() < 2: - return False - else: - if self._isLeaf(): - return True - elif self.left and self.right: - return self.left._isBalanced() and self.right._isBalanced() - elif not self.left and self.right: - return self.right._isBalanced() - elif not self.right and self.left: - return self.left._isBalanced() - - def _isBST(self, mintree=None, maxtree=None): - ''' Find whether the tree is a BST, inorder ''' - if self.item: - if not mintree: - mintree = self.item - if not maxtree: - maxtree = self.item - - if self._isLeaf(): - return True - elif self.left: - if self.left.item < self.item and mintree > self.left.item: - mintree = self.left.item - return self.left._isBST(mintree, maxtree) - else: - return False - elif self.right: - if self.right.item > self.item and maxtree < self.right.item: - maxtree = self.right.item - return self.right._isBST(mintree, maxtree) - else: - return False - else: - print('Tree is empty') - - - - - - - - -class BinaryTree(object): - - def __init__(self): - self.root = None - - - def addNode(self, value): - if not self.root: - self.root = NodeBT(value) - else: - self.root._addNextNode(value) - - - def isLeaf(self, value): - node = self.root._searchForNode(value) - if node: - return node._isLeaf() - else: - print "Node not found." - - - def getNodeLevel(self, item): - node = self.root._searchForNode(item) - if node: - return node.level - else: - print('Node not found') - - - def isRoot(self, value): - return self.root.item == value - - - def getHeight(self): - return self.root._getMaxHeight() - - - def isBalanced(self): - return self.root._isBalanced() - - - def isBST(self): - return self.root._isBST() - - - def preorder(self): - current = self.root - nodes, stack = [], [] - while stack or current: - if current: - nodes.append(current.item) # thats what change - stack.append(current) - current = current.left - else: - current = stack.pop() - current = current.right - return nodes - - - -if __name__ == '__main__': - bt = BinaryTree() - print "Adding nodes 1 to 10 in the tree..." - for i in range(1, 10): - bt.addNode(i) - print "Is 8 a leaf? ", bt.isLeaf(8) - print "Whats the level of node 8? ", bt.getNodeLevel(8) - print "Is node 10 a root? ", bt.isRoot(10) - print "Is node 1 a root? ", bt.isRoot(1) - print "Whats the tree height? ", bt.getHeight() - print "Is this tree BST? ", bt.isBST() - print "Is this tree balanced? ", bt.isBalanced() - print (bt.preorder()) diff --git a/src/extra_interview_problems/glassdoor/2014/trees/check_whether_bst.py b/src/extra_interview_problems/glassdoor/2014/trees/check_whether_bst.py deleted file mode 100755 index 7bc3eac..0000000 --- a/src/extra_interview_problems/glassdoor/2014/trees/check_whether_bst.py +++ /dev/null @@ -1,64 +0,0 @@ -__author__ = "bt3" - - -""" need to keep track of min and max!!!""" - -from binary_tree import BinaryTree, NodeBT -from binary_search_tree import BinarySearchTree, NodeBST - -## my solution - -def isBST(bt, mintree=None, maxtree=None): - if not bt.item: - return True - - if not mintree: - mintree = bt.item - - if not maxtree: - maxtree = bt.item - - left, right = False, False - - if bt.left: - if bt.left.item > bt.item and bt.left.item < mintree: - return False - else: - mintree = bt.left.item - left = isBST(bt.left, mintree, maxtree) - else: - left = True - if bt.right: - if bt.right.item < bt.item and bt.right.item > maxtree: - return False - else: - maxtree = bt.right.item - right = isBST(bt.right, mintree, maxtree) - else: - right = True - - return left and right - - - - - - - - - -if __name__ == '__main__': - bt = BinaryTree() - print "Adding nodes 1 to 10 in the tree..." - for i in range(1, 10): - bt.addNode(i) - - print(isBST(bt.root)) - - bst = BinarySearchTree() - print "Adding nodes 1 to 10 in the tree..." - for i in range(1, 10): - bst.addNode(i) - - print(isBST(bst.root)) - print(bst.isBST()) diff --git a/src/extra_interview_problems/glassdoor/2014/trees/lowest_common_ancestor.py b/src/extra_interview_problems/glassdoor/2014/trees/lowest_common_ancestor.py deleted file mode 100755 index bf5f0f2..0000000 --- a/src/extra_interview_problems/glassdoor/2014/trees/lowest_common_ancestor.py +++ /dev/null @@ -1,47 +0,0 @@ -#!/usr/bin/python - -__author__ = "bt3" - - -from binary_tree import BinaryTree - - -def inorder(node, path=None): - if node: - path = path or [] - path.append(node.item) - inorder(node.left, path) - inorder(node.right, path) - return path - - - -def lowest_commom_ancestor(node, node1, node2): - path = inorder(node.root) - i1, i2 = 0, 0 - for i,n in enumerate(path): - if n == node1: - i1 = i - if n == node2: - i2 = i - return path[i1:i2+1] - - - - - - -if __name__ == '__main__': - bt = BinaryTree() - l = [10, 6, 14, 3, 7, 11, 15] - for i in l: - bt.addNode(i) - - print(l) - print(lowest_commom_ancestor(bt, 10, 6)) - print(lowest_commom_ancestor(bt, 10, 14)) - print(lowest_commom_ancestor(bt, 10, 3)) - print(lowest_commom_ancestor(bt, 10, 7)) - print(lowest_commom_ancestor(bt, 10, 11)) - print(lowest_commom_ancestor(bt, 10, 15)) - diff --git a/src/extra_interview_problems/glassdoor/2014/trees/transversal_BST_ancestor.py b/src/extra_interview_problems/glassdoor/2014/trees/transversal_BST_ancestor.py deleted file mode 100755 index 75de17f..0000000 --- a/src/extra_interview_problems/glassdoor/2014/trees/transversal_BST_ancestor.py +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/python - -__author__ = "bt3" - - -''' find the lowest ancestor in a BST ''' - -from transversal_BST_recursively import BSTwithTransversalRecursively - - - -def find_ancestor(path, low_value, high_value): - while path: - current_value = path[0] - if current_value < low_value: - try: - path = path[2:] - except: - return current_value - elif current_value > high_value: - try: - path = path[1:] - except: - return current_value - elif low_value <= current_value <= high_value: - return current_value - else: - return None # this is probably never touched - - - - -if __name__ == '__main__': - bst = BSTwithTransversalRecursively() - l = [10, 5, 15, 1, 6, 11, 50] - for i in l: - bst.addNode(i) - path = bst.preorder() - print("The path inorder: ", path) - - print("The path between 1 and 6 is :", find_ancestor(path, 1, 6)) - print("The path between 1 and 11 is: ", find_ancestor(path, 1, 11)) - print("The path between 11 and 50 is: ", find_ancestor(path, 11, 50)) - print("The path between 5 and 15 is: ", find_ancestor(path, 5, 15)) diff --git a/src/extra_interview_problems/glassdoor/2015/check_if_bst.py b/src/extra_interview_problems/glassdoor/2015/check_if_bst.py deleted file mode 100644 index e9da054..0000000 --- a/src/extra_interview_problems/glassdoor/2015/check_if_bst.py +++ /dev/null @@ -1,56 +0,0 @@ -#!/usr/bin/env python - -__author__ = "bt3" - - -''' -Given a tree find out whether is a BST or not -''' - -class Tree(object): - def __init__(self, value): - self.value = value - self.right = None - self.left = None - -def create_tree_bst(): - tree = Tree(4) - tree.right = Tree(6) - tree.left = Tree(2) - tree.right.left = Tree(5) - tree.right.right = Tree(7) - tree.left.left = Tree(1) - tree.left.right = Tree(3) - return tree - -def create_tree_not_bst(): - tree = Tree(4) - tree.right = Tree(6) - tree.left = Tree(2) - tree.right.left = Tree(5) - tree.right.right = Tree(7) - tree.left.left = Tree(3) - tree.left.right = Tree(1) - return tree - - -INFINITY = float("infinity") -NEG_INFINITY = float("-infinity") - -def isBST(tree, minVal=NEG_INFINITY, maxVal=INFINITY): - if not tree: - return True - - if not minVal <= tree.value <= maxVal: - return False - - return isBST(tree.left, minVal, tree.value) and \ - isBST(tree.right, tree.value, maxVal) - - -if __name__ == '__main__': - tree = create_tree_bst() - print isBST(tree) - - tree = create_tree_not_bst() - print isBST(tree) diff --git a/src/extra_interview_problems/glassdoor/2015/alpha_permutation.py b/src/extra_interview_problems/math_arrays_and_strings/alpha_permutation.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2015/alpha_permutation.py rename to src/extra_interview_problems/math_arrays_and_strings/alpha_permutation.py diff --git a/src/extra_interview_problems/glassdoor/2015/anagram.py b/src/extra_interview_problems/math_arrays_and_strings/anagram.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2015/anagram.py rename to src/extra_interview_problems/math_arrays_and_strings/anagram.py diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/anagrams_hash_table.txt b/src/extra_interview_problems/math_arrays_and_strings/anagrams_hash_table.txt similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/anagrams_hash_table.txt rename to src/extra_interview_problems/math_arrays_and_strings/anagrams_hash_table.txt diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/balance.txt b/src/extra_interview_problems/math_arrays_and_strings/balance.txt similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/balance.txt rename to src/extra_interview_problems/math_arrays_and_strings/balance.txt diff --git a/src/extra_interview_problems/math_arrays_and_strings/balance_symbols.py b/src/extra_interview_problems/math_arrays_and_strings/balance_symbols.py new file mode 100644 index 0000000..ff65f82 --- /dev/null +++ b/src/extra_interview_problems/math_arrays_and_strings/balance_symbols.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python + +__author__ = "bt3" + +''' +Given a N different open and close braces in a string "( { [ } ] )". +How do you check whether the string has matching braces. +''' + +from collections import Counter +def check_if_balance(string): + ''' + >>> check_if_balance('{[[(])}]') + True + >>> check_if_balance('{[[()}]') + False + >>> check_if_balance('') + True + ''' + table = Counter() + for i in string: + + index = str(ord(i))[0] + if i in '{[(': + table[index] += 1 + + elif i in ')}]': + table[index] -= 1 + + for i in table.values(): + if i !=-0: + return False + return True + + + +if __name__ == '__main__': + import doctest + doctest.testmod() + diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/check_2_numbers_array_sum.py b/src/extra_interview_problems/math_arrays_and_strings/check_2_numbers_array_sum.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/check_2_numbers_array_sum.py rename to src/extra_interview_problems/math_arrays_and_strings/check_2_numbers_array_sum.py diff --git a/src/extra_interview_problems/glassdoor/2015/check_if_3_numbers_sum_to_zero.py b/src/extra_interview_problems/math_arrays_and_strings/check_if_3_numbers_sum_to_zero.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2015/check_if_3_numbers_sum_to_zero.py rename to src/extra_interview_problems/math_arrays_and_strings/check_if_3_numbers_sum_to_zero.py diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/check_if_anagrams.py b/src/extra_interview_problems/math_arrays_and_strings/check_if_anagrams.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/check_if_anagrams.py rename to src/extra_interview_problems/math_arrays_and_strings/check_if_anagrams.py diff --git a/src/extra_interview_problems/glassdoor/2015/check_if_palindrome.py b/src/extra_interview_problems/math_arrays_and_strings/check_if_palindrome.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2015/check_if_palindrome.py rename to src/extra_interview_problems/math_arrays_and_strings/check_if_palindrome.py diff --git a/src/extra_interview_problems/glassdoor/2015/check_non_overlapping_intervals.py b/src/extra_interview_problems/math_arrays_and_strings/check_non_overlapping_intervals.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2015/check_non_overlapping_intervals.py rename to src/extra_interview_problems/math_arrays_and_strings/check_non_overlapping_intervals.py diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/combinations.py b/src/extra_interview_problems/math_arrays_and_strings/combinations.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/combinations.py rename to src/extra_interview_problems/math_arrays_and_strings/combinations.py diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/delete_duplicate_char_str.py b/src/extra_interview_problems/math_arrays_and_strings/delete_duplicate_char_str.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/delete_duplicate_char_str.py rename to src/extra_interview_problems/math_arrays_and_strings/delete_duplicate_char_str.py diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/fibonacci.py b/src/extra_interview_problems/math_arrays_and_strings/fibonacci.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/fibonacci.py rename to src/extra_interview_problems/math_arrays_and_strings/fibonacci.py diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/find_if_is_substr.py b/src/extra_interview_problems/math_arrays_and_strings/find_if_is_substr.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/find_if_is_substr.py rename to src/extra_interview_problems/math_arrays_and_strings/find_if_is_substr.py diff --git a/src/extra_interview_problems/glassdoor/2015/find_if_numbers_sum_to_k.py b/src/extra_interview_problems/math_arrays_and_strings/find_if_numbers_sum_to_k.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2015/find_if_numbers_sum_to_k.py rename to src/extra_interview_problems/math_arrays_and_strings/find_if_numbers_sum_to_k.py diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/find_if_unique_char.py b/src/extra_interview_problems/math_arrays_and_strings/find_if_unique_char.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/find_if_unique_char.py rename to src/extra_interview_problems/math_arrays_and_strings/find_if_unique_char.py diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/find_largest_sum.py b/src/extra_interview_problems/math_arrays_and_strings/find_largest_sum.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/find_largest_sum.py rename to src/extra_interview_problems/math_arrays_and_strings/find_largest_sum.py diff --git a/src/extra_interview_problems/glassdoor/2015/find_longest_str_unique_chars.py b/src/extra_interview_problems/math_arrays_and_strings/find_longest_str_unique_chars.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2015/find_longest_str_unique_chars.py rename to src/extra_interview_problems/math_arrays_and_strings/find_longest_str_unique_chars.py diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/find_non_repeating_number.py b/src/extra_interview_problems/math_arrays_and_strings/find_non_repeating_number.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/find_non_repeating_number.py rename to src/extra_interview_problems/math_arrays_and_strings/find_non_repeating_number.py diff --git a/src/extra_interview_problems/glassdoor/2015/find_nth_fibonacci.py b/src/extra_interview_problems/math_arrays_and_strings/find_nth_fibonacci.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2015/find_nth_fibonacci.py rename to src/extra_interview_problems/math_arrays_and_strings/find_nth_fibonacci.py diff --git a/src/extra_interview_problems/glassdoor/2015/find_prime_factors.py b/src/extra_interview_problems/math_arrays_and_strings/find_prime_factors.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2015/find_prime_factors.py rename to src/extra_interview_problems/math_arrays_and_strings/find_prime_factors.py diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/finding_gcd.py b/src/extra_interview_problems/math_arrays_and_strings/finding_gcd.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/finding_gcd.py rename to src/extra_interview_problems/math_arrays_and_strings/finding_gcd.py diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/finding_if_prime.py b/src/extra_interview_problems/math_arrays_and_strings/finding_if_prime.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/finding_if_prime.py rename to src/extra_interview_problems/math_arrays_and_strings/finding_if_prime.py diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/generate_prime.py b/src/extra_interview_problems/math_arrays_and_strings/generate_prime.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/generate_prime.py rename to src/extra_interview_problems/math_arrays_and_strings/generate_prime.py diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/get_float_rep_bin.py b/src/extra_interview_problems/math_arrays_and_strings/get_float_rep_bin.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/get_float_rep_bin.py rename to src/extra_interview_problems/math_arrays_and_strings/get_float_rep_bin.py diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/interserction_two_arrays.py b/src/extra_interview_problems/math_arrays_and_strings/interserction_two_arrays.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/interserction_two_arrays.py rename to src/extra_interview_problems/math_arrays_and_strings/interserction_two_arrays.py diff --git a/src/extra_interview_problems/glassdoor/2015/longest_common_prefix.py b/src/extra_interview_problems/math_arrays_and_strings/longest_common_prefix.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2015/longest_common_prefix.py rename to src/extra_interview_problems/math_arrays_and_strings/longest_common_prefix.py diff --git a/src/extra_interview_problems/math_arrays_and_strings/max_subarray_stocks.py b/src/extra_interview_problems/math_arrays_and_strings/max_subarray_stocks.py new file mode 100755 index 0000000..e6f5f41 --- /dev/null +++ b/src/extra_interview_problems/math_arrays_and_strings/max_subarray_stocks.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +__author__ = "bt3" + + + +def beating_stock(array): + + imin = 0 + + # first deal is just buying in the next day (1) + deal = [array[1] - array[imin], imin, 1] + + for i, d in enumerate(array): + + deal_here = d - array[imin] + + if deal_here > deal[0]: + deal = [deal_here, imin, i] + + elif d < array[imin]: + imin = i + + return deal[0], array[deal[1]], array[deal[2]] + +if __name__ == '__main__': + array = [7, 2, 3, 6, 5, 8, 5, 3, 4] + deal = beating_stock(array) + print("Profit: %d, buying at %d, selling at %d." %(deal[0], deal[1], deal[2])) \ No newline at end of file diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/number_of_zeros_factorial.txt b/src/extra_interview_problems/math_arrays_and_strings/number_of_zeros_factorial.txt similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/number_of_zeros_factorial.txt rename to src/extra_interview_problems/math_arrays_and_strings/number_of_zeros_factorial.txt diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/permutations.py b/src/extra_interview_problems/math_arrays_and_strings/permutations.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/permutations.py rename to src/extra_interview_problems/math_arrays_and_strings/permutations.py diff --git a/src/extra_interview_problems/glassdoor/2015/permutations.py b/src/extra_interview_problems/math_arrays_and_strings/permutations2.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2015/permutations.py rename to src/extra_interview_problems/math_arrays_and_strings/permutations2.py diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/prod_other_ints.py b/src/extra_interview_problems/math_arrays_and_strings/prod_other_ints.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/prod_other_ints.py rename to src/extra_interview_problems/math_arrays_and_strings/prod_other_ints.py diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/ransom_note.py b/src/extra_interview_problems/math_arrays_and_strings/ransom_note.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/ransom_note.py rename to src/extra_interview_problems/math_arrays_and_strings/ransom_note.py diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/rev_string.py b/src/extra_interview_problems/math_arrays_and_strings/rev_string.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/rev_string.py rename to src/extra_interview_problems/math_arrays_and_strings/rev_string.py diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/reverse_words.py b/src/extra_interview_problems/math_arrays_and_strings/reverse_words.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/reverse_words.py rename to src/extra_interview_problems/math_arrays_and_strings/reverse_words.py diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/reverse_words_sentence.py b/src/extra_interview_problems/math_arrays_and_strings/reverse_words_sentence.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/reverse_words_sentence.py rename to src/extra_interview_problems/math_arrays_and_strings/reverse_words_sentence.py diff --git a/src/extra_interview_problems/glassdoor/2015/sum_two_numbers_as_strings.py b/src/extra_interview_problems/math_arrays_and_strings/sum_two_numbers_as_strings.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2015/sum_two_numbers_as_strings.py rename to src/extra_interview_problems/math_arrays_and_strings/sum_two_numbers_as_strings.py diff --git a/src/extra_interview_problems/glassdoor/2015/binary_search.py b/src/extra_interview_problems/sorting_and_searching/binary_search.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2015/binary_search.py rename to src/extra_interview_problems/sorting_and_searching/binary_search.py diff --git a/src/extra_interview_problems/glassdoor/2015/merge_and_sort_two_arrays.py b/src/extra_interview_problems/sorting_and_searching/merge_and_sort_two_arrays.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2015/merge_and_sort_two_arrays.py rename to src/extra_interview_problems/sorting_and_searching/merge_and_sort_two_arrays.py diff --git a/src/extra_interview_problems/glassdoor/2014/sorting_and_searching/quick_sort.py b/src/extra_interview_problems/sorting_and_searching/quick_sort.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2014/sorting_and_searching/quick_sort.py rename to src/extra_interview_problems/sorting_and_searching/quick_sort.py diff --git a/src/extra_interview_problems/trees/binary_search_tree.py b/src/extra_interview_problems/trees/binary_search_tree.py new file mode 100755 index 0000000..7fcc738 --- /dev/null +++ b/src/extra_interview_problems/trees/binary_search_tree.py @@ -0,0 +1,103 @@ +#!/usr/bin/python + +__author__ = "bt3" + + +class Node(object): + + def __init__(self, item=None,): + + self.item = item + self.left = None + self.right = None + + def __repr__(self): + return '{}'.format(self.item) + + + def _add(self, value): + new_node = Node(value) + + if not self.item: + self.item = new_node + + else: + if value > self.item: + self.right = self.right and self.right._add(value) or new_node + elif value < self.item: + self.left = self.left and self.left._add(value) or new_node + else: + print("BSTs do not support repeated items.") + + return self # this is necessary!!! + + + def _search(self, value): + if self.item == value: + return True # or self + + elif self.left and value < self.item: + return self.left._search(value) + + elif self.right and value > self.item: + return self.right._search(value) + + else: + return False + + + def _isLeaf(self): + return not self.right and not self.left + + + def _printPreorder(self): + print self.item + + if self.left: + return self.left._printPreorder() + + if self.right: + return self.right._printPreorder() + + + +class BST(object): + + def __init__(self): + self.root = None + + def add(self, value): + if not self.root: + self.root = Node(value) + else: + self.root._add(value) + + def printPreorder(self): + if self.root: + self.root._printPreorder() + + def search(self, value): + if self.root: + return self.root._search(value) + + + +if __name__ == '__main__': + + bst = BST() + print "Adding nodes 1 to 10 in the tree..." + for i in range(1, 11): + bst.add(i) + + print + print "Searching for nodes 16 and 6" + print bst.search(16) + print bst.search(6) + + print + print "Printing preorder..." + bst.printPreorder() + + + + diff --git a/src/extra_interview_problems/trees/binary_tree.py b/src/extra_interview_problems/trees/binary_tree.py new file mode 100755 index 0000000..75161d7 --- /dev/null +++ b/src/extra_interview_problems/trees/binary_tree.py @@ -0,0 +1,122 @@ +#!/usr/bin/python + +__author__ = "bt3" + + +class Node(object): + + def __init__(self, item=None,): + + self.item = item + self.left = None + self.right = None + + def __repr__(self): + return '{}'.format(self.item) + + + def _add(self, value): + new_node = Node(value) + + if not self.item: + self.item = new_node + elif not self.left: + self.left = new_node + elif not self.right: + self.right = new_node + else: + self.left = self.left._add(value) + return self + + + def _search(self, value): + if self.item == value: + return True # or self + + found = False # or False, thats diff from BST + if self.left: + found = self.left._search(value) + + if self.right: + found = found or self.right._search(value) + + return found + + + def _isLeaf(self): + return not self.right and not self.left + + + +class BT(object): + + def __init__(self): + self.root = None + + + def add(self, value): + if not self.root: + self.root = Node(value) + else: + self.root._add(value) + + + def printPreorder(self): + current = self.root + nodes, stack = [], [] + while stack or current: + if current: + nodes.append(current.item) # this is what change + stack.append(current) + current = current.left + else: + current = stack.pop() + current = current.right + print nodes + + + def printPostorder(self): + current = self.root + nodes, stack = [], [] + while stack or current: + + if current: + stack.append(current) + current = current.left + else: + current = stack.pop() + nodes.append(current.item) # this is what change + current = current.right + print nodes + + + def search(self, value): + if self.root: + return self.root._search(value) + + + + +if __name__ == '__main__': + + bt = BT() + print "Adding nodes 1 to 10 in the tree..." + for i in range(1, 11): + bt.add(i) + + print + print "Searching for nodes 16 and 6" + print bt.search(16) + print bt.search(6) + + print + print "Printing preorder..." + bt.printPreorder() + + print + print "Printing postorder..." + bt.printPostorder() + + print + print "Printing Inorder..." + bt.printInorder() \ No newline at end of file diff --git a/src/extra_interview_problems/trees/check_if_balanced.py b/src/extra_interview_problems/trees/check_if_balanced.py new file mode 100755 index 0000000..568cec0 --- /dev/null +++ b/src/extra_interview_problems/trees/check_if_balanced.py @@ -0,0 +1,30 @@ +__author__ = "bt3" + + +from binary_search_tree import BST, Node +from binary_tree import BT, Node + + + +def isBalanced(node, left=0, right=0): + if not node: + return (left - right) < 2 + + return isBalanced(node.left, left+1, right) and \ + isBalanced(node.right, left, right+1) + + + + +if __name__ == '__main__': + bt = BST() + for i in range(1, 10): + bt.add(i) + + assert(isBalanced(bt.root) == True) + + bt = BT() + for i in range(1, 10): + bt.add(i) + + assert(isBalanced(bt.root) == False) diff --git a/src/extra_interview_problems/trees/check_if_bst.py b/src/extra_interview_problems/trees/check_if_bst.py new file mode 100755 index 0000000..b089ef0 --- /dev/null +++ b/src/extra_interview_problems/trees/check_if_bst.py @@ -0,0 +1,62 @@ +__author__ = "bt3" + + +from binary_search_tree import BST, Node +from binary_tree import BT, Node + + +def isBST(node, min_node=float("-infinity"), maxVal=float("infinity")): + if not node: + return True + + if not min_node <= node.item <= maxVal: + return False + + return isBST(node.left, min_node, node.item) and \ + isBST(node.right, node.item, maxVal) + + + +def isBST_other_method(node, max_node=None, min_node=None): + + if not node: + return True + + left, right = True, True + min_node = min_node or float('inf') + max_node = max_node or -float('inf') + + if node.left: + if node.left.item > node.item or node.left.item > max_node: + left = False + else: + max_node = node.item + left = isBST(node.left, max_node, min_node) + + if node.right: + if node.right.item < node.item or node.right.item < min_node: + rihjt = False + else: + min_node = node.item + right = isBST(node.right, max_node, min_node) + + return left and right + + + + + +if __name__ == '__main__': + bt = BST() + for i in range(1, 10): + bt.add(i) + + assert(isBST(bt.root) == True) + + bt = BT() + for i in range(1, 10): + bt.add(i) + + assert(isBST(bt.root) == False) + + diff --git a/src/extra_interview_problems/glassdoor/2015/transversing_bt.py b/src/extra_interview_problems/trees/transversing_bt.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2015/transversing_bt.py rename to src/extra_interview_problems/trees/transversing_bt.py diff --git a/src/extra_interview_problems/glassdoor/2015/trie.py b/src/extra_interview_problems/trees/trie.py similarity index 100% rename from src/extra_interview_problems/glassdoor/2015/trie.py rename to src/extra_interview_problems/trees/trie.py