Skip to content

Instantly share code, notes, and snippets.

@zedchance
zedchance / books.py
Created December 13, 2021 23:25
Divide books ordered on a bookshelf so each person reads roughly the same amount
# book partitioning so each person reads roughly the same amount
import math
def solve(pages, people):
n = len(pages)
m = [[0 for j in range(people)] for i in range(n)]
s = [[-1 for j in range(people)] for i in range(n)]
# work left to right
@zedchance
zedchance / Makefile
Created December 6, 2021 18:20
Page swap algorithm simulator
all: vmem
vmem: vmem.c
clang vmem.c -o vmem
test: vmem input1.txt
./vmem input1.txt LRU
./vmem input1.txt CLOCK
test2: vmem input2.txt
@zedchance
zedchance / assembly_mod.py
Last active December 3, 2021 22:14
Fastest path thru assembly line, with consecutive station bonus
# assembly line scheduling problem, modified with consecutive station bonus
import math
C_FACTOR = 1
def solve(e1, e2, a1, a2, t1, t2, x1, x2):
""" finds fastest route thru factory """
assert len(a1) == len(a2)
n = len(a1)
@zedchance
zedchance / matrix_mul.py
Last active December 8, 2021 19:20
Optimal parenthesization of matrix multiplication
# optimized parenthesization of matrix multiplication
import math
def print_2d(arr):
for row in arr:
for col in row:
if col == math.inf:
print(col, end=" ")
@zedchance
zedchance / lcs.py
Last active November 18, 2021 00:31
Longest common subsequence
# longest common subsequence
def lcs(x, y):
""" returns longest common sequence length, exponential runtime complexity """
if len(x) == 0 or len(y) == 0:
return 0
elif x[-1] == y[-1]:
return lcs(x[:-1], y[:-1]) + 1
else:
return max(lcs(x[:-1], y),
@zedchance
zedchance / Makefile
Last active December 18, 2021 04:49
Bounded buffer synchronization problem
all: buffer
buffer: buffer.h buffer.c
clang buffer.c -o buffer -lpthread
test: buffer
./buffer 1 1 5
test2: buffer
./buffer 20 20 20
@zedchance
zedchance / Makefile
Last active November 9, 2021 05:45
CPU scheduling simulator
all: simulator
simulator: simulator.o queue.o
clang simulator.o queue.o -o simulator
simulator.o: simulator.c simulator.h
clang -g -c simulator.c -Wall
queue.o: queue.c simulator.h
clang -g -c queue.c -Wall
@zedchance
zedchance / median.py
Last active October 7, 2021 05:08
Finds low median of 2 sorted arrays
import unittest
def median(a, b):
""" finds the low median of 2 sorted arrays, a and b """
def _median(a, b, index):
if index == 0:
if len(a) == 0:
return b[0]
@zedchance
zedchance / Makefile
Last active October 24, 2021 16:33
Observe linux system information
all: observer
observer: observer.c
clang observer.c -o observer
clean:
rm -f observer
test: observer
./observer
@zedchance
zedchance / sudoku.py
Last active September 28, 2021 01:44
Backtracking sudoku solver
# Sudoku puzzle solver
def make_sudoku_board():
return [
[5, 3, 0, 0, 7, 0, 0, 0, 0],
[6, 0, 0, 1, 9, 5, 0, 0, 0],
[0, 9, 8, 0, 0, 0, 0, 6, 0],
[8, 0, 0, 0, 6, 0, 0, 0, 3],
[4, 0, 0, 8, 0, 3, 0, 0, 1],