Skip to content

Commit

Permalink
Refactored solutions 253 - 254
Browse files Browse the repository at this point in the history
  • Loading branch information
WHAHA-HA committed Jan 31, 2021
1 parent 0874b27 commit 3016499
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 55 deletions.
46 changes: 27 additions & 19 deletions Solutions/253.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""
Problem:
Given a string and a number of lines k, print the string in zigzag form. In zigzag, characters are printed out diagonally from top left to bottom right until reaching the kth line, then back up to top right, and so on.
Given a string and a number of lines k, print the string in zigzag form. In zigzag,
characters are printed out diagonally from top left to bottom right until reaching the
kth line, then back up to top right, and so on.
For example, given the sentence "thisisazigzag" and k = 4, you should print:
Expand All @@ -12,23 +14,21 @@
"""


def clamp(num, min_value, max_value):
# function to clamp the number with-in the given range
def clamp(num: int, min_value: int, max_value: int) -> int:
return max(min(num, max_value), min_value)


def print_zigzag(string, k):
def print_zigzag_string(string: str, k: int) -> None:
if k < 1:
return

length = len(string)
# storing the sting in a matrix
mat = [[" " for _ in range(length)] for _ in range(k)]
matrix = [[" " for _ in range(length)] for _ in range(k)]
i, j = 0, 0
is_increasing = True

# generating the string matrix
# generating zigzag string matrix
for char in string:
mat[i][j] = char
matrix[i][j] = char
j += 1
if is_increasing:
i += 1
Expand All @@ -40,18 +40,26 @@ def print_zigzag(string, k):
i = clamp(i - 2, 0, k - 1)
else:
i = clamp(i + 2, 0, k - 1)
# printing the string matrix
for row in mat:
# displaying the string matrix
for row in matrix:
for elem in row:
print(elem, end="")
print()


# DRIVER CODE
print_zigzag("thisisazigzag", 4)
print()
print_zigzag("thisisazigzag", 3)
print()
print_zigzag("thisisazigzag", 2)
print()
print_zigzag("thisisazigzag", 1)
if __name__ == "__main__":
print_zigzag_string("thisisazigzag", 4)
print()
print_zigzag_string("thisisazigzag", 3)
print()
print_zigzag_string("thisisazigzag", 2)
print()
print_zigzag_string("thisisazigzag", 1)


"""
SPECS:
TIME COMPLEXITY: O(n x k)
SPACE COMPLEXITY: O(n x k)
"""
81 changes: 45 additions & 36 deletions Solutions/254.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"""
Probelm:
Recall that a full binary tree is one in which each node is either a leaf node, or has two children. Given a binary tree, convert it to a full one by removing nodes with only one child.
Recall that a full binary tree is one in which each node is either a leaf node, or has
two children. Given a binary tree, convert it to a full one by removing nodes with only
one child.
For example, given the following tree:
a
Expand All @@ -12,61 +15,67 @@
\ / \
f g h
You should convert it to:
a
/ \
f e
/ \
g h
"""

from DataStructures.Tree import Node, Binary_Tree
from DataStructures.Tree import Node, BinaryTree


def create_full_bin_tree_helper(self):
# helper function to generate the full binary tree
def create_full_bin_tree_helper(node: Node) -> None:
# if a node with one missing child is encountered, the value is replaced by its
# child and the children of the current node overwrittrn with the child's children
if self.right is None and self.left is None:
# child and the children of the current node overwritten with the child's children
if node.right is None and node.left is None:
return
elif self.left is not None and self.right is None:
self.val = self.left.val
self.right = self.left.right
self.left = self.left.left
self.create_full_bin_tree_helper()
elif self.left is None and self.right is not None:
self.val = self.right.val
self.left = self.right.left
self.right = self.right.right
self.create_full_bin_tree_helper()
elif self.left is not None and self.right is not None:
self.left.create_full_bin_tree_helper()
self.right.create_full_bin_tree_helper()
elif node.left is not None and node.right is None:
node.val = node.left.val
node.right = node.left.right
node.left = node.left.left
create_full_bin_tree_helper(node)
elif node.left is None and node.right is not None:
node.val = node.right.val
node.left = node.right.left
node.right = node.right.right
create_full_bin_tree_helper(node)
elif node.left is not None and node.right is not None:
create_full_bin_tree_helper(node.left)
create_full_bin_tree_helper(node.right)


def create_full_bin_tree(tree: BinaryTree) -> None:
if tree.root:
create_full_bin_tree_helper(tree.root)

def create_full_bin_tree(self):
self.root.create_full_bin_tree_helper()

if __name__ == "__main__":
tree = BinaryTree()
tree.root = Node("a")

setattr(Node, "create_full_bin_tree_helper", create_full_bin_tree_helper)
setattr(Binary_Tree, "create_full_bin_tree", create_full_bin_tree)
tree.root.left = Node("b")
tree.root.right = Node("c")

tree.root.left.left = Node("d")
tree.root.left.left.right = Node("f")

# DRIVER CODE
tree = Binary_Tree()
tree.root.right.right = Node("e")

tree.root = Node("a")
tree.root.left = Node("b")
tree.root.right = Node("c")
tree.root.right.right.left = Node("g")
tree.root.right.right.right = Node("h")

tree.root.left.left = Node("d")
tree.root.left.left.right = Node("f")
print(tree)

tree.root.right.right = Node("e")
tree.root.right.right.left = Node("g")
tree.root.right.right.right = Node("h")
create_full_bin_tree(tree)

print(tree)
print(tree)

tree.create_full_bin_tree()

print(tree)
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(log(n))
"""

0 comments on commit 3016499

Please sign in to comment.