View randomMocks.test.js
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)
View pi.pyth
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
View interpolation.coffee
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
View golf.js
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)
}
View explanation.pyth
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
View fixed_explain.coffee
# 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
View fixed.coffee
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
View gist:ca1366cb9b954d1b48d9
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
View gist:5b5a72e47549b522e126
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)