This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
NewerOlder