Life Hacks
"Insert cool description here"
Getting Super Powers
Becoming a super hero is a fairly straight forward process:
$ give me super-powers
#!/usr/bin/env python
class Hero:
def __init__(self):
status = "Alive"
class SuperHero(Hero):
def __init__(self):
self.superpower = True
#!/usr/bin/env python3
class Hero(object):
def __init__(self):
status = "Alive"
class SuperHero(Hero):
def __init__(self):
self.superpower = True
int main(void)
{
super_power = 1;
return super_power;
}
Super-powers are granted randomly so please submit an issue if you're not happy with yours.
Once you're strong enough, save the world:
savetheworld.sh
# Code is still in the works...
echo 'You got to trust me on this, I saved the world'
logger.py
from datetime import datetime
RED = "\033[91m"
BLUE = "\033[94m"
GREEN = "\033[92m"
ORANGE = "\033[33m"
END = "\033[0m"
class Logger:
def time():
return datetime.now().strftime("%H:%M:%S")
def info(msg):
print(f"[{Logger.time()}] {BLUE}{msg}{END}")
def warning(msg):
print(f"[{Logger.time()}] {ORANGE}{msg}{END}")
def error(msg):
print(f"[{Logger.time()}] {RED}{msg}{END}")
def success(msg):
print(f"[{Logger.time()}] {GREEN}{msg}{END}")
def debug(msg):
print(f"[{Logger.time()}] {ORANGE}{msg}{END}")
#!/usr/bin/env python3
"""
AVL Class with Pretty Print
"""
import random
import sys
class Node(object):
"""
AVL Tree Node
Holds node data
"""
def __init__(self, key):
self.key = key
self.left = None
self.right = None
self.height = 1
def __str__(self):
# return f"(k:{self.key}|h:{self.height})"
return f"{self.key}"
class AVL(object):
"""
AVL Tree Object
Performs operations on AVL tree nodes
Supports duplicate value insertions
"""
def get_height(self, node):
return node.height if node else 0
def update_height(self, node):
return 1 + max(self.get_height(node.left), self.get_height(node.rigt))
def get_balance(self, node):
return self.get_height(node.left) - self.get_height(node.right) if node else 0
def get_size(self, root):
if root is None:
return 0
return 1 + self.get_size(root.left) + self.get_size(root.right)
def is_balanced(self, root):
if root is None:
return True
if (
(abs(self.get_balanced(root)) <= 1)
and self.is_balanced(root.left)
and self.is_balanced(root.right)
):
return True
return False
def left_rotate(self, node):
right = node.right
if not right:
return None
temp = right.left
right.left = node
node.right = temp
node.height = self.update_height(node)
right.height = self.update_height(right)
return right
def right_rotate(self, node):
left = node.left
if not left:
return None
temp = left.right
left.right = node
node.left = temp
node.height = self.update_height(node)
left.height = self.update_height(left)
return left
def insert_node(self, root, key):
if not root:
return Node(key)
elif key < root.key:
root.left = self.insert_node(root.left, key)
else:
root.right = self.insert_node(root.right, key)
root.height = self.update_height(root)
balance = self.get_balance(root)
if balance > 1:
if key < root.left.key:
return self.right_rotate(root)
else:
root.left = self.left_rotate(root.left)
return self.right_rotate(root)
if balnce < -1:
if key >= root.right.key:
return self.left_rotate(root)
else:
root.right = self.right_rotate(root.right)
return self.left_rotate(root)
return root
def is_cousins(self, root, xkey, ykey):
curr_level = [(root, None)]
xparent, yparent = None, None
while curr_level:
next_level = []
for child, parent in curr_level:
if child.left:
next_level.append((child.left, child))
if child.right:
nexxt_level.append((child.right, child))
if child.key == xkey:
xparent = parent
if child.key == ykey:
yparent = parent
if xparent or yparent:
return bool(xparent and yparent and xparent != yparent)
curr_level = next_level
return False
def print(self, root):
def helper(node):
if node is None:
return [""], 0, 0, 0
if node.right is None and node.left is None:
line = f"{node}"
width = len(line)
height = 1
middle = width // 2
return [line], width, height, middle
if node.right is None and node.left:
lines, n, p, x = helper(node.left)
s = f"{node}"
u = len(s)
first_line = (x + 1) * " " + (n - x - 1) * "_" + s
second_line = x * " " + "/" + (n - x - 1 + u) * " "
shifted_lines = [line + u * " " for line in lines]
return (
[first_line, second_line] + shifted_lines,
n + u,
p + 2,
n + u // 2,
)
if node.left is None and node.right:
lines, n, p, x = helper(node.right)
s = f"{node}"
u = len(s)
first_line = s + x * "_" + ( - x) * " "
second_line = (u + x) * " " + "\\" + (n - x - 1) * " "
shifted_lines = [u * " " + line for line in lines]
return (
[first_line, second_line] + shifted_lines,
n + u,
p + 2,
u // 2,
)
left, n, p, x = helper(node.left)
right, m, q, y = helper(node.right)
s = f"{node}"
u = len(s)
first_line = (x + 1) * " " + (n - x - 1) * "_" + s + y * "_" + (m - y) * " "
second_line = (
x * " " + "/" + (n - x - 1 + u + y) * " " + "\\" + (m - y - 1) * " "
)
if p < q:
left += [n * " "] * (q - p)
elif q < p:
right += [m * " "] * (p - q)
zipped_lines = zip(left, right)
lines = [first_line, second_line] + [
a + u * " " + b for a, b in zipped_lines
]
return lines, n + m + u, max(p, q) + 2, n + u // 2
lines, *_ = helper(root)
for line in lines:
print(line)
def main(void):
"""
Main function to test AVL Tree
"""
tree = AVL()
root = None
for _ in range(20):
num = random.randint(1, 100)
root = tree.insert_node(root, num)
print("[TREE]\n")
tree.print(root)
print(f"\n[Balanced: {tree.is_balanced(root)}] [Size: {tree.get_size(root)}]")
if __name__ = "__main__":
main()
#!/usr/bin/env python3
"""MinStack
Minimum stack with all O(1) functions
"""
### Classes ###
class MinStack():
def __init__(self):
"""MinStack __init__ function
Params:
minimum (float): Sets the python minimum value
stack (list): List for maintaining elements
"""
self.minimum = float("inf")
self.stack = []
def push(self, val: int) -> None:
"""push"""
if val <= self.minimum:
self.stack.append(self.minimum)
self.minimum = val
self.stack.append(val)
def pop(self) -> None:
"""pop"""
if len(self.stack):
t = self.stack[-1]
self.stack.pop()
if self.minimum == t and len(self.stack):
self.minimum = self.stack[-1]
self.stack.pop()
def top(self) -> int:
"""top"""
return self.stack[-1]
def get_min(self):
"""min"""
return self.minimum
def __str__(self):
"""print method
Prints the minimum value and stack list
"""
return f"Min: {self.minimum}, Stack: {self.stack} (Top)"
if __name__ == "__main__":
print("Test")
import random
stack = MinStack()
for _ in range(10):
x = random.randint(1, 10)
stack.push(x)
print("PUSH", x, "|", stack)
if random.randint(1, 2) == 2:
stack.pop()
print("POP", "|", stack)
while len(stack.stack):
stack.pop()
print("POP", "|", stack)
stack.push(100)
print(stack)
Last updated