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) |
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 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 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 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 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
var moneyTypes = [5000, 1000, 500, 100, 50, 30] | |
var lims = { | |
5000: 4, | |
1000: 5, | |
500: 2, | |
100: 5, | |
50: 100, | |
30: 23 | |
} |
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 genBrackets = function(n) { | |
const result = [] | |
if ( n > 0 ) { | |
genBrackets.add(n, 0, "", result) | |
} | |
return result.join(" ") | |
} |
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
// ABAZDC, BACBAD => ABAD | |
// AGGTAB, GXTXAYB => GTAB | |
// aaaa, aa => aa | |
const getLongestSubseq = function(s1, s2) { | |
let longest = null | |
for ( let s1from=0; s1from<s1.length; ++s1from ) { | |
let current = [] | |
let s1i = s1from, s2i = 0 |
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 rotateWords = function(arr) { | |
rotateWords.reverse(arr, 0, arr.length-1) | |
let firstSpaceIndex = arr.indexOf(" ") | |
let secondSpaceIndex = arr.indexOf(" ", firstSpaceIndex + 1) | |
rotateWords.reverse(arr, 0, firstSpaceIndex - 1) | |
rotateWords.reverse(arr, firstSpaceIndex + 1, secondSpaceIndex - 1) | |
rotateWords.reverse(arr, secondSpaceIndex + 1, arr.length - 1) |
OlderNewer