Skip to content

Instantly share code, notes, and snippets.

View e-mihaylin's full-sized avatar
🏠
Working from home

Eugene Mihaylin e-mihaylin

🏠
Working from home
View GitHub Profile
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);
}
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)) {
//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;
}
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]);
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;
};
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);
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]);
}
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;
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;
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