Last active
March 16, 2018 06:02
-
-
Save toanant/be87988bfc25c99841a808781df081dc to your computer and use it in GitHub Desktop.
Collection of analytical problems solution in python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Binary Tree Creation and Traversal | |
from queue import Queue | |
class BinaryTree(object): | |
def __init__(self, value): | |
self.value = value | |
self.left_child = None | |
self.right_child = None | |
def insert_left(self, value): | |
if self.left_child is None: | |
self.left_child = BinaryTree(value) | |
else: | |
new_node = BinaryTree(value) | |
new_node.left_child = self.left_child | |
self.left_child = new_node | |
def insert_right(self, value): | |
if self.right_child is None: | |
self.right_child = BinaryTree(value) | |
else: | |
new_node = BinaryTree(value) | |
new_node.right_child = self.right_child | |
self.right_child = new_node | |
def pre_order(self): | |
print(self.value) | |
if self.left_child: | |
self.left_child.pre_order() | |
if self.right_child: | |
self.right_child.pre_order() | |
def in_order(self): | |
if self.left_child: | |
self.left_child.in_order() | |
print(self.value) | |
if self.right_child: | |
self.right_child.in_order() | |
def post_order(self): | |
if self.left_child: | |
self.left_child.post_order() | |
if self.right_child: | |
self.right_child.post_order() | |
print(self.value) | |
def bfs(self): | |
queue = Queue() | |
queue.put(self) | |
while not queue.empty(): | |
current_node = queue.get() | |
print(current_node.value) | |
if current_node.left_child: | |
queue.put(current_node.left_child) | |
if current_node.right_child: | |
queue.put(current_node.right_child) | |
def __str__(self): | |
return self.in_order() | |
# Check balanced parenthesis | |
def check_balanced(input_str): | |
push_char, pop_char = "([{", ")]}" | |
stack = [] | |
for c in input_str: | |
if c in push_char: | |
stack.append(c) | |
elif c in pop_char: | |
if not stack: | |
return False | |
else: | |
top = stack.pop() | |
balance_char = push_char[pop_char.index(c)] | |
if balance_char != top: | |
return False | |
return not stack | |
# Check for anagram | |
def is_anagram(s, t): | |
s = list(s) | |
s.sort() | |
return s == t | |
def check_anagram(s, t): | |
match_length = len(t) | |
pattern_length = len(s) | |
t = list(t) | |
t.sort() | |
for i in range(pattern_length - match_length + 1): | |
if is_anagram(s[i:i + match_length], t): | |
return True | |
return False | |
# Check for longest palindrome | |
def longest_palindrome(s): | |
longest, left, right = 0, 0, 0 | |
size = len(s) | |
for i in range(0, size): | |
for j in range(i + 1, size + 1): | |
substr = s[i:j] | |
if substr == substr[::-1] and len(substr) > longest: | |
longest = len(substr) | |
left, right = i, j | |
result = s[left:right] | |
return result | |
# Matrix Spiral print | |
def print_spiral(ar): | |
if not ar: | |
return | |
print " ".join(str(a) for a in ar[0]) | |
ar = zip(*[reversed(row) for row in ar[1:]]) | |
print_spiral(ar) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment