Skip to content

Instantly share code, notes, and snippets.

View iJustErikk's full-sized avatar

Erik Awwad iJustErikk

View GitHub Profile
@iJustErikk
iJustErikk / scheduler.py
Created September 30, 2022 01:27
Broadway Snippet
# This was modified from a class assignment
# I did the following:
# - translated from c++ to python
# - optimized broadcast to signal via condition variable map to reduce unnecessary wakeups and context switches
# - switched shortest seek first to combined metric to also optimize on resource starvation
# Since this was translated, there might be some inconsistencies. Nonetheless, I can produce high quality software.
# Areas to be improved: docs/comments, unit/functional testing, static analysis (hopefully for threading) and quality linting
# globals could be raised to service and communication could happen over channels or other higher level synchronization primitives
# This was tested 20k times to check for numerous thread interleavings
// really easy with set interval
let currentTime = 10;
const ONE_SECOND = 1000;
const intervalId = setInterval(() => {
console.log(currentTime--);
if (currentTime === 0) clearInterval(intervalId);
}, ONE_SECOND);
// recursive function wrapper
function isTreeSymmetric(t) {
// empty tree is automatically symmetric
if (!t) return true;
return isTreeSymmetricRecursive(t.left, t.right);
}
function isTreeSymmetricRecursive(left, right) {
// implementation detail
// if we dont have children, we are symmetric ourselves
if (!(left || right)) return true;
const isPalindrome = (head) => {
// zero and single element lists are always symmetric
if (head === null || head.next === null) return true;
let fast = head;
let slow = head;
// fast pointer technique
// since fast traverses whole list and moves 2x as fast
// it makes sense how slow can be only halfway through
// if list is odd, fast will end up being not being null
// checks two moving indices and stops when needed
const isPalindrome = (arr) => {
// symmetry will always be found in one or 0 elements
// [1] -> true
// [] -> true
if (arr.length < 2) return true;
// taking two pointer approach
let first = 0;
let last = arr.length - 1;
while (first < last) {
@iJustErikk
iJustErikk / isarraypalindrome.js
Created December 15, 2020 14:27
For medium article.
// checks two moving indices and stops when needed
const isPalindrome = (arr) => {
// symmetry will always be found in one or 0 elements
// [1] -> true
// [] -> true
if (arr.length < 2) return true;
// taking two pointer approach
let first = 0;
let last = arr.length - 1;
while (first < last) {
@iJustErikk
iJustErikk / islistpalimdrome.js
Last active December 15, 2020 16:19
updated for medium
const isPalindrome = (head) => {
// zero and single element lists are always symmetric
if (head === null || head.next === null) return true;
let fast = head;
let slow = head;
// fast pointer technique
// since fast traverses whole list and moves 2x as fast
// it makes sense how slow can be only halfway through
// if list is odd, fast will end up being not being null
@iJustErikk
iJustErikk / 01knapsack.js
Last active December 11, 2020 01:18
fixed bug
const knapsack = (vals, weights, limit, memo = new Map()) => {
// sub problems are going to be whether we take something or
if (vals.length === 0) return 0;
if (memo.has(`${vals}${weights}`)) return memo.get(`${vals}${weights}`);
const currentWeight = weights[0];
const currentVal = vals[0];
const leftOverWeight = limit - currentWeight;
// if we cant take first item
if (leftOverWeight < 0) return knapsack(vals.slice(1), weights.slice(1), limit, memo);
// first call is we dont take it, second we do
@iJustErikk
iJustErikk / nqueensbacktracking.js
Created December 11, 2020 00:47
SOO HAPPY I DID THIS ONE
const printBoard = (board) => {
const length = board.length;
for (let row = 0; row < length; row++) {
let str = "";
for (let col = 0; col < length; col++) {
const current = board[row][col];
current ? str += "Q" : str += "X";
}
console.log(str);
}