Skip to content

Instantly share code, notes, and snippets.

View violet-athena's full-sized avatar

Danielle Tinkov violet-athena

View GitHub Profile
@violet-athena
violet-athena / getCharacterFrequency.js
Last active August 12, 2019 16:11
Find the the most frequent character in a string
const getCharacterFrequency = string => string.split('').reduce((acc, char) => {
const { length } = string.split(char);
if (length > acc.frequency) {
acc.frequency = length;
acc.mostFrequent = char;
}
return acc;
}, { frequency: 0, mostFrequent: '' }).mostFrequent;
@violet-athena
violet-athena / findPeaks.js
Created May 11, 2019 15:53
Find peaks including plateaus in a 1D array
const findPeaks = arr => arr.reduce((result, curr, i) => {
const prev = arr[i - 1];
const next = arr.slice(i).find(item => item !== curr);
if(curr > prev && curr > next) {
result.pos.push(i);
result.peaks.push(curr);
}
return result;
@violet-athena
violet-athena / sumOfArray.js
Created November 28, 2016 01:21
Javascript Sum of Array in one line
// manual implementation
const sum = ([first, ...rest]) => rest.length ? first + sum(rest) : first;
// using reduce
const sum = arr => arr.reduce((acc, el) => acc + el, 0);
@violet-athena
violet-athena / fibonacci.js
Last active April 27, 2018 11:46
Doing Fibonacci, iterative vs. functional
// imperative
function fibonacci(n, first = 0, second = 1) {
while (n !== 0) {
console.log(first); // side-effect
[n, first, second] = [n - 1, second, first + second]; // assignment
}
}
fibonacci(10);
// functional
@violet-athena
violet-athena / fibonacci.coffee
Last active October 2, 2015 20:58
Node.js non-blocking Fibonacci written on CoffeeScript
###
Original project here: https://github.com/glenjamin/node-fib ,
this is CoffeeScript rewrite of the app. Nothing more, nothing less.
###
app = require("express").createServer()
port = "3000"
fibonacci = (n, callback) ->
inner = (n1, n2, i) ->