Skip to content

Instantly share code, notes, and snippets.

Avatar

harrytallbelt

View GitHub Profile
@harrytallbelt
harrytallbelt / anagrams.py
Last active Jun 10, 2020
Finds all anagrams for given words (Python 3).
View anagrams.py
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 / transpose-in-place.js
Last active Jun 10, 2020
M*N matrix transposition in place (O(1) memory) in JavaScript and Python.
View transpose-in-place.js
// 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 / create-bare-git-repo.sh
Last active Nov 8, 2017
Create a local bare repo, you can (put in Dropbox folder) use like remote.
View create-bare-git-repo.sh
# 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 / commands.sh
Created Oct 31, 2017
helpful bash commands
View commands.sh
# 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 / matrix.js
Created Oct 25, 2017
A satisfying Matrix effect with Node.JS.
View matrix.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 / format-simplify.js
Created Oct 6, 2017
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
View format-simplify.js
// 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 / convert-to-grayscale.sh
Created Mar 20, 2017
Bash script to convert coloured photos to (much smaller) grayscale using imagemagick package.
View convert-to-grayscale.sh
#!/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 / waterCapacity.fs
Created May 18, 2016
F# solution for water on land problem
View waterCapacity.fs
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
You can’t perform that action at this time.