Skip to content

Instantly share code, notes, and snippets.

@harrytallbelt
harrytallbelt / waterCapacity.fs
Created May 18, 2016 15:44
F# solution for water on land problem
let oneWayCapacity a =
let mutable max = 0
a |> List.map (fun e -> if e > max then max <- e; 0 else max - e)
let totalCapacity a =
let aL = a |> oneWayCapacity
let aR = a |> List.rev |> oneWayCapacity |> List.rev
List.map2 min aL aR |> List.sum
@harrytallbelt
harrytallbelt / convert-to-grayscale.sh
Created March 20, 2017 22:59
Bash script to convert coloured photos to (much smaller) grayscale using imagemagick package.
#!/bin/bash
mkdir results
for image in *.jpg
do
convert -colorspace Gray -interlace Plane -gaussian-blur 0.05 -quality 50% $image ./results/$image
done
@harrytallbelt
harrytallbelt / format-simplify.js
Created October 6, 2017 21:52
A bit of code for pretty formatting Simplify predicates. It can be used to parse and format s-lists if you throw a few lines out
// Indentation unit: predicate components are indented with this string,
// repeated several times, depending on their nesting level.
// Use '' for no identation.
const DEFAULT_INDENT_STR = ' '
// All lines end with this string; predicates on the top level
// are separated with twice this string.
// Use ' ' for a one-line output ('' won't lead to valid predicates).
const DEFAULT_LINE_ENDING = '\n'
// An s-list shorter than this would be a one-liner.
// This number is not exact, though (see `estimatedLength`).
@harrytallbelt
harrytallbelt / matrix.js
Created October 25, 2017 18:24
A satisfying Matrix effect with Node.JS.
const LINE_WIDTH = 80 // 150
const CONTINUATION_PROBABILITY = 0.9
const FRAMERATE = 100
console.log('\x1b[32m') // green text
console.log('\x1b[40m') // black bg
let line = []
for (let i = 0; i < LINE_WIDTH / 2; ++i) {
line.push(false)
@harrytallbelt
harrytallbelt / commands.sh
Created October 31, 2017 16:07
helpful bash commands
# count lines in all inner js files of a project
cat `find . | grep -v 'node_modules' | grep '.*.js$'` | wc -l
# list all the js files that contain a word
grep -v 'node_modules' | grep '.*.js$' | xargs grep 'SEARCHWORD' -l
@harrytallbelt
harrytallbelt / create-bare-git-repo.sh
Last active November 8, 2017 13:44
Create a local bare repo, you can (put in Dropbox folder) use like remote.
# Create a new bare repo.
cd origin-parh
mkdir repo.git
cd repo.git
git --bare init
# Make an already existing repo bare.
cd origin-parh
mkdir repo
cd repo
@harrytallbelt
harrytallbelt / transpose-in-place.js
Last active June 10, 2020 17:12
M*N matrix transposition in place (O(1) memory) in JavaScript and Python.
// matrix -- a 1D array, that stores matrix A[rows,columns] by rows
// i.e. [A[0,0],A[0,1],..,A[0,columns-1],A[1,0],A[1,1],..,A[1,columns-1],..]
// rows -- number of rows in A
// columns -- number of columns in A
function transpose(matrix, rows, columns) {
// Let's write down some index conversion functions.
// You can convert 1D array index to
// (rows*columns) matrix index like that
const C = k => [Math.floor(k / columns), k % columns]
// You can do the reverse this way:
@harrytallbelt
harrytallbelt / anagrams.py
Last active June 10, 2020 17:09
Finds all anagrams for given words (Python 3).
import math
import itertools
# Tested with dictionary from github.com/dwyl/english-words
WORDS_DICT_FILENAME = 'words_dictionary.json'
__dict = None
def get_valid_words():
global __dict
if __dict:
@harrytallbelt
harrytallbelt / excel-row-codes.py
Created April 14, 2021 13:28
Excel-like row index to letter code conversion
import math
# 1, 2, 3, .. 26, 27, ... -> 'a', 'b', 'c', .. 'z', 'aa', ...
def to_row_code(row_index):
prefix = ''
if row_index > 26:
prefix = to_row_code(math.ceil((row_index - 26) / 26))
return prefix + chr(ord('a') + (row_index - 1) % 26)