Skip to content

Instantly share code, notes, and snippets.

@johnwahba
johnwahba / worker.mjs
Created December 23, 2023 02:27
worker pattern nodejs
const CONCURRENCY = 5;
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const doWork = async (work) => {
const duration = Math.random() > 0.5 ? 1000 : 10000
await sleep(duration)
return console.log("doing work", work)
}
@johnwahba
johnwahba / leetcode_etl.rb
Last active October 6, 2023 01:22
Create an sqlite db of your leetcode visits and submissions to track progress
# Script runs on mac. Would need to be adapted to run on windows.
require 'sqlite3'
require 'json'
require 'zip'
require 'tmpdir'
require 'find'
# Function to unzip the file
def unzip_file(file, destination)
<:Title>
New Poll
<:Body>
<h1>New Poll</h1>
<form>
<h3>Question</h3>
<input type="text" value="{{ page._todo._question }}">
{{ page._todo._answers.each do |answer| }}
<div><input type="text" value="{{ answer._text }}"></div>
Alternating 2 opposite walls and 2 adjacent walls 4 times guarantees freedom for the subset of problems where half of the buttons are in each position.
Then flip a random switch and the problem has been reduced to one that has already been solved.
@johnwahba
johnwahba / frontend.html
Last active December 27, 2015 19:29
Everlane files
<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<body></body>
<script src="frontend.js"></script>
<script src="frontendDev.js"></script>
</html>
@johnwahba
johnwahba / ruby_cipher.rb
Created October 30, 2013 19:35
Cipher implementation ruby
def rotx(x, string, encrypt=true)
offset = encrypt ? x : -x
uppers = Hash[("A".."Z").zip(("A".."Z").to_a.rotate(offset))]
lowers = Hash[("a".."z").zip(("a".."z").to_a.rotate(offset))]
dictionary = uppers.merge(lowers)
output = ""
string.length.times do |idx|
char = string[idx]
if dictionary[char]
output += dictionary[char]
@johnwahba
johnwahba / promise.js
Last active December 27, 2015 00:29
Basic promise implementation
Promise = function(){
this.result = undefined;
this.complete = false
this.failure = false;
this.onFailure = [];
this.onSuccess = [];
};
Promise.prototype.resolve = function(fnResult){
if (this.complete){