Skip to content

Instantly share code, notes, and snippets.

View sylvainpolletvillard's full-sized avatar

Sylvain Pollet-Villard sylvainpolletvillard

View GitHub Profile
export const formatBytes = (precision = 2) => bytes => {
if (typeof bytes !== "number") return bytes
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toPrecision(precision)) + ' ' + sizes[i];
@sylvainpolletvillard
sylvainpolletvillard / fourmi.js
Created March 5, 2020 12:18
quick langton ant
const parseGrid = ([str]) => str
.split('\n')
.map(row => row.trim())
.filter(row => row.length > 0)
.map(row => row.split('').map(char => char !== '.'))
const formatGrid = (rows, ant) => rows
.map((row, y) => row.map((cell, x) => {
if (ant.x === x && ant.y === y) {
return '8'
function timer(delay){
const subscribers = []
let count = 0
const interval = setInterval(() => {
subscribers.forEach(observer => observer.next(count))
count++;
}, delay)
const timerObservable = {
subscribe: observer => {
@sylvainpolletvillard
sylvainpolletvillard / solution.js
Created February 15, 2021 21:49
pinapple pen interview question
/*
Given a string str and a dictionary dict containing a list of non-empty words, add spaces in str to construct a “sentence” where each word is a valid word in dict. Return all possible sentences. You are allowed to reuse the words in dict.
Example:
str = “penpineapplepenapple”
dict = [“apple”, “pen”, “applepen”, “pine”, “pineapple”]
$ makeSentence(str, dict) $ [ “pen pine apple pen apple”, “pen pineapple pen apple”, “pen pine applepen apple” ]
*/
function makeSentence(str, dict){
const findWordsAtStart = str => dict.filter(word => str.startsWith(word))