Skip to content

Instantly share code, notes, and snippets.

@timhuff
timhuff / gist:5b5a72e47549b522e126
Created February 19, 2015 12:44
Algorithm for testing if two strings of the form /[A-Z][a-z]*/ are anagrams
isAnagram = (a,b)->
if a.length != b.length
return false
[a,b] = [a,b].map (x)->x.toLowerCase().split('')
length = a.length
if length > 12852
throw new Error "This algorithm can only reliably handle strings smaller than 12,852 characters"
anagramHash = (hash, letter)->
charCode = letter.charCodeAt(0)-96
if !(1 <= charCode <= 26)
prefixString = "* + 2 3 4"
evaluatePrefix = (prefix)->
operator = prefix.shift()
leftValue = if isNaN parseFloat prefix[0] then evaluatePrefix prefix else prefix.shift()
rightValue = if isNaN parseFloat prefix[0] then evaluatePrefix prefix else prefix.shift()
eval leftValue+operator+rightValue
prefix = prefixString.split ' '
console.log evaluatePrefix prefix
@timhuff
timhuff / fixed.coffee
Last active September 12, 2016 08:26
l=Math.log;r=((n)->z=[0..l(n)/l 2].reduce(((a,b)->`1<<b&n?3+a:4+a`),0);`z==n?n:r(z)`);c={};[1..1000000].map((n)->c[r n]?=0;c[r n]++);c
# set l to Math.log because you use it twice
l = Math.log
# create a recursive function, r, to iterate on a chain until it reaches a fixed point
# n is the input for the current iteration
r = (n)->
###
This next line is a mouthful. "l(n)/l 2" computes the log base 2 of n. This is the index of the highest set bit in n
The reduce function is basically a sum across the bits of the number.
It adds 3 to the sum if the bit is set ("one".length) and 4 if the bit is not set ("zero".length)
So, in a nutshell, z is set to the number of letters in the binary phrase for the number n
L?-b=dsm?.&.<1db3 4hlbydb@mXdH1mydSQ0
L create lambda function named y with param b
? ternary if
- minus used for expressing "if d != b"
b lambda param
= assign
d d =
s sum
m map
@timhuff
timhuff / golf.js
Last active September 13, 2016 01:48
y = function(b){
nums = []
for(i=0; i<=Math.floor(Math.log(b)/Math.log(2)); i++)
nums.push(i);
d = nums.reduce(function(n, a){1<<n&b?3+a:4+a},0)
if(b == d)
return b
else
return y(b)
}
numBars = 73
bars = [0..numBars-1].map -> 0
getDisplayBars = (audioData)->
deltaT = audioData.length/numBars
bars[0] = audioData[0]
for barNum in [1..numBars-1]
timeIndex = barNum*deltaT
leftIndex = Math.floor timeIndex
A.Q@c6csm?-i+OG1+1OG1Z1HH2
Two commands:
A .Q - Parse the input (.Q), assign 1st line (max dice number) to G and 2nd line (number of trials) to H
@c6csm?-i+OG1+1OG1Z1HH2 - The output
The output:
@c6csm?-i+OG1+1OG1Z1HH2
@ c6csm?-i+OG1+1OG1Z1HH 2 - Take the square root of the middle chunk
c 6 csm?-i+OG1+1OG1Z1HH - Divide 6 by the 3rd chunk
const crypto = require('crypto')
describe('Predictable Randomness', () => {
describe('Math.random', () => {
const firstResult = 0.37454011430963874
const secondResult = 0.7965429842006415
beforeEach(() => Math.__clearChances__())
it('should produce these two numbers first by default', () => {
expect(Math.random()).toEqual(firstResult)
expect(Math.random()).toEqual(secondResult)
[
"L2 F2 R2 D' L2 F2 U' L2 D' L2 U B D B2 D R U' F' L B' F R",
"L2 D2 B2 U R2 F2 L2 D2 B2 D B2 F U' L U2 B2 D' L F2 L U2",
"D F2 U' F2 R2 B2 L2 U' B2 R2 U' L2 R D' B U' F U' B' L R U",
"D2 B2 U' B2 U' R2 D F2 U2 L2 F2 D' R' U' R2 D' B2 F' U L2 D2 F'",
"B2 U F2 D F2 D2 F2 D B2 L2 B2 R D2 F U' L' F' D' L F R F2",
"D' B2 R2 F2 U' R2 D' B2 U2 F2 D' U2 F' U B' R' F L' B2 R2 U' F",
"L2 B2 U F2 U' B2 F2 U2 B2 L2 U B2 F' U' L' B' F' L' U' B2 L2 U2",
"B2 D2 L2 F2 D2 L2 U B2 F2 U2 B' R' B' U F' R B' F2 U F2 D2 R",
"R2 D2 L2 U B2 D' B2 D' L2 U L2 U' B R' D' R' D' R2 D2 U2 B R'",