Skip to content

Instantly share code, notes, and snippets.

View rinchik's full-sized avatar
🎯
Focusing

rinchik rinchik

🎯
Focusing
View GitHub Profile
const makeCoordinatesZeroBased = rawCoordinates => Object.entries(rawCoordinates)
.reduce((processedCoordinates, [coordinate, value]) => {
const isCoordinateValid = typeof value === 'number';
let updatedValue = value;
if (isCoordinateValid)
updatedValue = value - 1;
const makeCoordinatesZeroBased = rawCoordinates => Object.entries(rawCoordinates)
.reduce((processedCoordinates, [coordinate, value]) => {
const shouldConvert = typeof value === 'number';
const updatedValue = shouldConvert ? value - 1 : value;
processedCoordinates[coordinate] = updatedValue;
return processedCoordinates;
}, {});
@rinchik
rinchik / convert.js
Created November 1, 2020 18:02
JS poorly readable code example:
const convert = input => Object.entries(input).reduce((acc, [k, v]) => {
if (typeof v === 'number') {
acc[k] = v - 1
} else {
acc[k] = v
}
return acc
}, {});
@rinchik
rinchik / fibonacciBinet.js
Created January 28, 2017 20:31
Fibonacci calculation with Binet's formula in JavaScript
function fibonacci(number) {
var sqRootOf5 = Math.sqrt(5);
var Phi = (1+sqRootOf5)/2;
var phi = (1-sqRootOf5)/2
return Math.round((Math.pow(Phi, number) - Math.pow(phi, number)) / sqRootOf5);
}
@rinchik
rinchik / FibonacciGenerator.js
Created January 28, 2017 19:54
FibonacciGenerator.js
function* fibonacci(number) {
var previous_first = 0, previous_second = 1, next = 1;
while(true) {
next = previous_first + previous_second;
previous_first = previous_second;
previous_second = next;
yield next;
@rinchik
rinchik / FibonacciHashLookup.js
Last active January 28, 2017 19:16
FibonacciHashLookup.js
function fibonacci(number) {
var sequence = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89];
var numberZeroBased = number - 1;
if (numberZeroBased > sequence.length)
throw new Error('The number you provided is outside of the range');
return sequence[numberZeroBased];
};
@rinchik
rinchik / FibonacciIteration.js
Last active January 28, 2017 18:38
FibonacciIteration.js
function fibonacci(number) {
var previous_first = 0, previous_second = 1, next = 1;
for(var i = 2; i <= number; i++) {
next = previous_first + previous_second;
previous_first = previous_second;
previous_second = next;
}
return next;
@rinchik
rinchik / FibonacciMemoization.js
Created January 28, 2017 18:08
FibonacciMemoization.js
var cache = {};
function fibonacci(number) {
if (number < 1)
return 0;
if (number <= 2)
return 1;
@rinchik
rinchik / FibonacciRecursion.js
Last active January 28, 2017 18:14
FibonacciRecursion.js
function fibonacci(number) {
if (number < 1)
return 0;
if (number <= 2)
return 1;
return fibonacci(number - 1) + fibonacci(number - 2);
}
@rinchik
rinchik / FibonacciLoop.js
Last active January 28, 2017 17:39
FibonacciLoop.js
function fibonacci(number) {
var sequence = [1, 1];
for (var i = 2; i < number; i++) {
sequence[i] = sequence[i-1]+ sequence[i-2];
}
return sequence[number-1];
}