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
| function stripUrlParams(url, paramsToStrip){ | |
| let [urlPath, urlParams] = url.split('?'); | |
| let paramKVMap = {}; | |
| let urlDistinctParams = '' | |
| if (urlParams) { | |
| urlParams.split('&').forEach((e) => { | |
| let [key, val] = e.split('='); | |
| if (!paramsToStrip || !paramsToStrip.includes(key)) { | |
| if (!paramKVMap[key]) { |
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
| // http://stackoverflow.com/questions/8002252/euler-project-18-approach | |
| function longestSlideDown (pyramid) { | |
| let pyramidSum = []; | |
| pyramid.forEach((r, i) => { | |
| pyramidSum.push(r.map((e) => { | |
| return (i === pyramid.length - 1) ? e : 0; | |
| })); | |
| }); | |
| for (let i = pyramidSum.length - 2; i >= 0; i--) { |
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
| function convert(input, source, target) { | |
| if (source === target) { | |
| return input; | |
| } | |
| // Compute the value of the input in source base | |
| // then convert it to value in target base | |
| let sourceValue = input.split('').reverse().reduce( (acc, val, digitPos) => { | |
| return acc + Math.pow(source.length, digitPos) * source.indexOf(val); | |
| }, 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
| function sierpinski(n) { | |
| if (n === 0) { | |
| return 'L'; | |
| } | |
| let tri = sierpinski(n - 1); | |
| let triRows = tri.split('\n'); | |
| let gap = 2 * triRows.length - 1; | |
| let arr = triRows.reduce((arr, r) => { | |
| arr.push(r + ' '.repeat(gap) + r); |
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
| function decode(str) { | |
| var result = []; | |
| let idx = 0; | |
| let charCount = 0; | |
| let strCharCount = ''; | |
| while (idx < str.length) { | |
| if (str[idx] === '\\') { | |
| // parse digits | |
| strCharCount = ''; | |
| idx += 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
| var maxSequence = function(arr){ | |
| let isAllNegative = arr.every((e) => e < 0); | |
| if (arr.length === 0 || isAllNegative) { | |
| return 0; | |
| } | |
| let isAllPositive = arr.every((e) => e > 0); | |
| if (isAllPositive) { | |
| return arr.reduce((a,b) => { return a + b }, 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
| array_diff = (a, b) -> | |
| diff_array = [] | |
| for e in a | |
| if e not in b then diff_array.push e | |
| diff_array |
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
| printerError = (s) -> | |
| goodAlphabet = "abcdefghijklm" | |
| errorCount = 0 | |
| for x in s | |
| if x not in goodAlphabet then errorCount += 1 | |
| "#{errorCount}/#{s.length}" |
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
| function chainFunctions(fns) { | |
| for (let fname in fns) { | |
| let originalFn = fns[fname]; | |
| let fn = (...inputs) => { | |
| if (this.result != null) { | |
| inputs.splice(0, 0, this.result); | |
| } | |
| let cloneCF = new chainFunctions(fns); | |
| cloneCF.result = originalFn.apply(this, inputs); | |
| return cloneCF |
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
| //https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_Recently_Used_.28LRU.29 | |
| // http://stackoverflow.com/questions/17438595/js-non-enumerable-function | |
| function LRUCache(capacity, init) { | |
| this.nextTime = 0; | |
| this.storage = []; | |
| this.initialCapacity = capacity; | |
| Object.defineProperty(this, 'nextTime', { enumerable: false }); | |
| Object.defineProperty(this, 'storage', { enumerable: false }); | |
| Object.defineProperty(this, 'initialCapacity', { enumerable: false }); |