Skip to content

Instantly share code, notes, and snippets.

View nma's full-sized avatar

Nick Ma nma

View GitHub Profile
@nma
nma / main.tf
Last active October 28, 2021 16:59
Vsphere Terraform VApp Example
terraform {
required_providers {
vsphere = {
source = "hashicorp/vsphere"
version = "1.11"
}
}
}
provider "vsphere" {
def rob_houses_dp_no_arr(house: List[int]) -> int:
if len(house) == 0: return 0
if len(house) == 1: return house[0]
max_so_far = house[0]
max_2_houses_ago = 0
for i in len(1, len(house)):
new_max = max(max_so_far, house[i] + max_2_houses_ago)
max_2_houses_ago = max_so_far
def rob_houses_dp(house: Tuple[int]) -> int:
if len(house) == 0: return 0
if len(house) == 1: return house[0]
dp = [-1] * len(house) # [-1, -1, -1, -1] default to undesirable value
# value of the first house
dp[0] = house[0]
for i in len(1, len(house)):
dp[i] = max(
def rob_houses_mem(house: Tuple[int], mem: Dict[int, int]) -> int:
if len(house) == 0: return 0
if hash(house) in mem:
return mem[hash(house)]
criminal_gains = max(
rob_houses(house[1:], mem),
house[1] + rob_houses(house[2:], mem)
)
def rob_houses(house: Tuple[int]) -> int:
if len(house) == 0: return 0
if len(house) == 1: return house[0]
criminal_gains = max(
rob_houses(house[1:]),
house[1] + rob_houses(house[2:])
)
return criminal_gains
@nma
nma / lca_parent_ptr_set.py
Created March 15, 2020 19:59
LCA algorithm with a parent pointer and a set to save time
def find_lca(n1: BinaryTreeNode, n2: BinaryTreeNode) -> Optional[BinaryTreeNode]:
seen_on_path: Set[BinaryTreeNode] = set()
while n1 or n2:
# go up the tree at the same time
if n1:
# at every point check if we have already seen the node
if n1 in seen_on_path:
# first seen will be an ancestor
@nma
nma / lca_bst.py
Last active March 23, 2020 14:46
LCA algorithm in a BST
# n1.data <= n2.data
def find_lca(tree: BstNode, n1: BstNode, n2: BstNode) -> Optional[BstNode]:
while tree:
# both left and right child are greater than current node
if n1.data > tree.data and n2.data > tree.data:
tree = tree.right
# both left and right child are less than the current node
elif n1.data < tree.data and n2.data < tree.data:
tree = tree.left
@nma
nma / lca_parent_ptr.py
Last active March 15, 2020 19:55
Lowest Common Ancestor Algorithm with Parent Pointers
def find_lca(n1: BinaryTreeNode, n2: BinaryTreeNode) -> BinaryTreeNode:
def get_depth(node: BinaryTreeNode) -> int:
depth = 0
while node and node.parent:
node = node.parent
depth += 1
return depth
d1 = get_depth(n1)
d2 = get_depth(n2)
from dataclasses import dataclass
@dataclass
class TraversalRecord:
seen_nodes: int
ancestor: BinaryTreeNode
def post_order_traversal_helper(node: BinaryTreeNode, n1: BinaryTreeNode, n2: BinaryTreeNode) -> TraversalRecord:
'''
@nma
nma / erd_example.puml
Last active June 28, 2019 15:31 — forked from QuantumGhost/example.puml
A simple template for PlantUML to draw ER diagram.The basic idea comes from http://plantuml.sourceforge.net/qa/?qa=331/database-modeling
@startuml
' uncomment the line below if you're using computer with a retina display
' skinparam dpi 300
!define Table(name,desc) class name as "desc" << (T,#FFAAAA) >>
' we use bold for primary key
' green color for unique
' and underscore for not_null
!define primary_key(x) <b>x</b>
!define unique(x) <color:green>x</color>
!define not_null(x) <u>x</u>