Skip to content

Instantly share code, notes, and snippets.

View Beraliv's full-sized avatar
📺
Contributing to OSS 7-9am, 7-9pm UK time (respond within a week)

Alexey Berezin Beraliv

📺
Contributing to OSS 7-9am, 7-9pm UK time (respond within a week)
View GitHub Profile
@Beraliv
Beraliv / alfabank.js
Last active May 25, 2018 18:43
Alfa bank. Which quantity of roubles is needed to buy euros for the lowest prices at the moment.
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
@Beraliv
Beraliv / anagramNumber.js
Last active April 10, 2018 16:51
Task 84. UniLecs. Find the number of anagrams.
// 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)
}
@Beraliv
Beraliv / digitalRoot.js
Last active March 16, 2018 07:09
Task 78. UniLecs. Find a digital root of the number. It equals to sum of all digits until the length is 1.
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
@Beraliv
Beraliv / permutation.js
Last active March 7, 2018 08:25
Task 76. UniLecs. Check minimal missing value of the permutation. Only natural numbers are considered. The size of the input array is not more than 10000.
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
@Beraliv
Beraliv / countOfZeros.js
Last active February 9, 2018 07:35
Task 69. UniLecs. Count the number of zeros at the end of the factorial of N, 1 <= N <= 10^9
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);
@Beraliv
Beraliv / twoDigits.js
Last active February 9, 2018 06:00
Task 68. UniLecs. Count of n digit numbers using only 4 and 7, where cannot be 3 same digits in a row.
/*
* 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)
@Beraliv
Beraliv / curried-flow-generator.js
Created January 22, 2018 21:28
Generator of Curried function types for index.js.flow
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>`;
}
@Beraliv
Beraliv / curried-ts-generator.js
Last active January 22, 2018 20:49
Generator of Curried function interface for index.d.ts
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>`;
}
// 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> {
@Beraliv
Beraliv / bigGifts.js
Last active January 17, 2018 21:36
Task 62. UniLecs. Functional-like. Added BigInteger.
const BigInteger = value => {
const normalise = n => {
if ((n | 0) === 0) {
return '0';
}
n = n.toString().split('');
while (+n[0] === 0) {
n.splice(0, 1);