This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getAnagrams = function(...args) { | |
const anagrams = [] | |
const candidates = {} | |
for ( let word of args ) { | |
let wordNorm = getAnagrams.normalize(word) | |
if ( candidates[wordNorm] ) { | |
anagrams.push([word, candidates[wordNorm]]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const check = function(seq) { | |
let opened = [] | |
for ( let char of seq ) { | |
if ( check.openers[char] ) { | |
opened.push(char) | |
} else { // closers | |
if ( opened.length==0 || opened[opened.length-1] !== check.closers[char] ) { | |
return false | |
} else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getPrimes = function(n) { | |
const result = [] | |
for ( let i=2;i<=n;++i ) { | |
if ( getPrimes.isPrime(i) ) { | |
result.push(i) | |
} | |
} | |
return result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Stack = function(data = []) { | |
this.data = data | |
} | |
Stack.prototype.push = function(value) { | |
this.data.push(value) | |
} | |
Stack.prototype.pop = function() { | |
return this.data.pop() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const example = "(2+2.0) *2" | |
const calculator = expr => { | |
while ( expr.match(/\(/) ) { | |
expr = expr.replace(/\(([^\)]+)\)/g, (_,subExpr) => calculator(subExpr)) | |
} | |
while ( expr.match(/[\*\/]/) ) { | |
expr = expr.replace(/([0-9\.]+)\s*([\*\/])\s*([0-9\.]+)/g, calculator.process) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const addOne = arr => arr.reduceRight((acc, v, i) => { | |
if ( i==arr.length-1 || arr[i+1] == 9 ) { | |
if ( v == 9 ) { | |
acc.push(0) | |
if ( i==0 ) { | |
acc.push(1) | |
} | |
} else { | |
acc.push(v+1) |
NewerOlder