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
function digitAt(index, amount) { | |
if (index <= 0) { | |
throw new TypeError('index must be positive') | |
} | |
return Math.floor(amount * Math.pow(10, index) % 10) | |
} | |
function restAfter(index, amount) { | |
const indent = Math.pow(10, index) | |
const rest = amount * indent % indent |
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
// it can be optimised via lazy evaluation or cached values | |
function factorial(n) { | |
if (n < 0) { | |
throw new TypeError('n should be non-negative') | |
} | |
if (n < 2) { | |
return 1 | |
} | |
return n * factorial(n - 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 digitalRoot = n => { | |
return Math.abs((n - 1) % 9 + 1) | |
} | |
console.log(digitalRoot(1)) // should be 1 | |
console.log(digitalRoot(11)) // should be 2 | |
console.log(digitalRoot(19)) // should be 1 | |
console.log(digitalRoot(191)) // should be 2 | |
console.log(digitalRoot(1918)) // should be 1 | |
console.log(digitalRoot(65536)) // should be 7 |
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 permutation = (arr) => { | |
let max | |
const set = [] | |
// set maximum and set | |
for (let i = 0; i < arr.length; i++) { | |
const element = arr[i] | |
// update maximum of elements | |
if (!max || max < element) { | |
max = element |
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 fact = n => n < 1 | |
? 1 | |
: Array(n) | |
.fill(0) | |
.map((_, i) => i + 1) | |
.reduce((a, v) => a * v, 1); | |
const countOf5 = n => Math.floor((n + 5) / 10); | |
const countOf10 = n => Math.floor(n / 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
/* | |
* It's not technically correct as there are possible numbers | |
* that have more than one subsequence of exact digits: 7774777 | |
* but here it's considered only one possible subsequence of exact digits | |
*/ | |
const answer = n => { | |
const allPossibleCount = n === 0 ? 0 : Math.pow(2, n); | |
const threeOrMoreSameDigitInARowCount = Array(Math.max(n - (3 + index) + 1, 0)) | |
.fill(0) | |
.map((_, index) => (n - 3 + index) * 2) |
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 g = n => { | |
const array = Array.from(Array(n), (_, i) => i + 1); | |
const indices = Array.from(Array(n + 1), (_, i) => i); | |
const returnF = k => { | |
const types = array.slice(k).map(t => `T${t}`); | |
return types.length === 0 | |
? 'R' | |
: `CurriedFunction${n - k}<${types.join(', ')}, R>`; | |
} |
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 g = n => { | |
const array = Array.from(Array(n), (_, i) => i + 1); | |
const indices = Array.from(Array(n + 1), (_, i) => i); | |
const returnF = k => { | |
const types = array.slice(k).map(t => `T${t}`); | |
return types.length === 0 | |
? 'R' | |
: `CurriedFunction${n - k}<${types.join(', ')}, R>`; | |
} |
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://raw.githubusercontent.com/donnut/typescript-ramda/master/ramda.d.ts | |
// https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/master/lodash/lodash.d.ts | |
declare namespace fp { | |
interface Dictionary<T> { | |
[index: string]: T; | |
} | |
interface CurriedFunction1<T1, R> { |
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 BigInteger = value => { | |
const normalise = n => { | |
if ((n | 0) === 0) { | |
return '0'; | |
} | |
n = n.toString().split(''); | |
while (+n[0] === 0) { | |
n.splice(0, 1); |