Skip to content

Instantly share code, notes, and snippets.

View nathanesau's full-sized avatar
😃

Nathan Esau nathanesau

😃
View GitHub Profile
@nathanesau
nathanesau / game_of_life.py
Created September 23, 2021 18:23
game_of_life
import time
import copy
import curses
screen = curses.initscr()
# sleep between generations
SLEEP_TIME = 0.01
# https://www.conwaylife.com/wiki/R-pentomino
@nathanesau
nathanesau / bridges_problem.py
Last active September 22, 2021 17:47
bridges_problem
import copy
def bfs(graph, start): # return visited
q = [start]
visited = {start}
while q:
node = q.pop(0)
# if we were searching for target
# we would check here
for neighbor in graph[node]:
@nathanesau
nathanesau / synonyms_sentences_problem.py
Created September 22, 2021 00:44
synonyms_sentences_problem
# generate all possible sentences
import copy
def hashdict(dict):
return hash(frozenset(dict.items()))
def neighbors(node, synoynms):
# all sentences one away
neighbors = []
for k, v in node.items():
@nathanesau
nathanesau / bs.py
Created May 17, 2021 00:08
matrix binary search
"""
write an efficient algorithm that searches a target value in an m x n integer matrix. the matrix has the following properties:
* integers in each row are sorted in ascending order from left to right.
* integers in each column are sorted in ascending order from top to bottom.
"""
def binary_search(arr, target):
left = 0
right = len(arr) - 1
while left <= right:
mid = (left+right)//2
@nathanesau
nathanesau / iced_rust_build_commands.md
Last active January 27, 2021 03:20
iced rust build commands

prerequisites:

# install wasm-pack from https://rustwasm.github.io/wasm-pack/installer/
cargo install wasm-bindgen-cli
rustup target add wasm32-unknown-unknown

build commands:

def solution_2(stops):
base_ts = 0
step = 1
for index, stop in enumerate(stops):
if stop == 'x':
continue
stop = int(stop)
ts = base_ts
while True:
if (ts + index) % stop == 0:
def solution_1(timestamp, stops):
closest_stop, closest_stop_timestamp = None, None
for stop in stops:
if stop == 'x':
continue
stop = int(stop)
stop_timestamp = timestamp + (stop - timestamp % stop)
if closest_stop_timestamp is None or \
(stop_timestamp - timestamp) < (closest_stop_timestamp - timestamp):
closest_stop, closest_stop_timestamp = stop, stop_timestamp
def rotate_cw(pos, value):
dir = 'E' if pos[0] > 0 else 'W' if pos[0] < 0 else 'N' if pos[1] > 0 else 'S'
order = ['E', 'S', 'W', 'N']
shift = value % 360 // 90
index = order.index(dir)
dir = order[(index + shift) % 4]
pv = abs(pos[0]) + abs(pos[1])
return [pv, 0] if dir == 'E' else [-pv, 0] if dir == 'W' else [0, pv] if dir == 'N' else [0, -pv]
def solution_1(arr, initial_dir="E"):
pos = [0, 0] # x, y
dir = initial_dir
for move in arr:
type, value = move[0], int(move[1:])
if type == 'F':
pos = [pos[0] + value, pos[1]] if dir == 'E' else [pos[0] - value, pos[1]] if dir == 'W' else \
[pos[0], pos[1] + value] if dir == 'N' else [pos[0], pos[1] - value]
if type == 'E':
pos = [pos[0] + value, pos[1]]
from subprocess import Popen
from subprocess import call
import time
import os
import win32gui
FFMPEG_PATH = "C:/ffmpeg/bin/ffmpeg.exe"
OUTFILE = "out.avi"