This file contains 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 lcm = (...a) => { | |
const gcd = (a, b) => b === 0 ? a : gcd(b, a % b); | |
const lcm = (a, b) => a * b / gcd(a, b); | |
return a.reduce(lcm); | |
} |
This file contains 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
class Calculator { | |
evaluate (s) { | |
s = s.replace(/\s/g, ''); | |
const l = s.length; | |
const stack = []; | |
let i = 0; | |
let o = '+'; | |
while (i < l) { | |
const x = s[i]; | |
if (/\d/.test(x)) { |
This file contains 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://www.codewars.com/kata/596d34df24a04ee1e3000a25 | |
const countOnes = (a, b) => cab(b) - cab(a - 1); | |
const cab = n => { | |
let bits = 0; | |
for (let i = 0; i < 30; ++i) | |
bits += ccb(i, n); | |
return bits; | |
} |
This file contains 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 justify = (s, length) => { | |
s = s.replace(/\n/g, ' ') | |
s = s.split` `; | |
const res = []; | |
let line = []; | |
let words = 0; | |
for (let i = 0; i < s.length; i++) { | |
if (words + line.length - 1 + s[i].length < length) { | |
line.push(s[i]); |
This file contains 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 calc = s => run(s.replace(/\s+/g, '').split``); | |
const run = s => { | |
let result = term(s); | |
while (peek(s) == '+' || peek(s) == '-') { | |
if (get(s) == '+') result += term(s); | |
else result -= term(s); | |
} | |
return result; | |
}; |
This file contains 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 count = n => Math.floor((ln(2 * Math.PI * n) / 2 + n * (ln(n) - 1)) / ln(10)) + 1; | |
const ln = x => Math.log(x) / Math.log(Math.E); |
This file contains 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 getPascalsTriangleRow = n => { | |
const a = []; | |
let x = 0; | |
for (let i = 0; i < n; i++) { | |
x = a.length - i; | |
for (let j = 0; j < i + 1; j++) | |
(j === 0 || j === i) ? a.push(1) : | |
a.push(a[x + j] + a[x + j - 1]); | |
} |
This file contains 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 multiply = (a, b) => { | |
const stack = []; | |
a = a.split``.reverse(); | |
b = b.split``.reverse(); | |
for (let i = 0, la = a.length; i < la; i++) { | |
for (let j = 0, lb = b.length; j < lb; j++) { | |
const m = a[i] * b[j]; | |
const s = stack[i + j]; | |
stack[i + j] = s ? s + m : m; |
This file contains 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 factorial = n => { | |
if (!n) return '1' | |
let i, next, carret; | |
const result = n.toString().split``.reverse().map(Number); | |
while (--n) { | |
i = carret = 0; | |
while ((next = result[i++]) !== undefined || carret) { | |
carret += n * (next || 0); | |
result[i - 1] = carret % 10; |
This file contains 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 longestSlideDown = p => p.reduceRight((x, e) => e.map((v, i) => v + Math.max(x[i], x[i + 1])))[0]; | |
// https://www.codewars.com/kata/551f23362ff852e2ab000037 |
NewerOlder