Skip to content

Instantly share code, notes, and snippets.

@Dammmien
Dammmien / kadane.js
Created February 15, 2016 18:09
Kadane algorithm In computer science, the maximum subarray problem is the task of finding the contiguous subarray within a one-dimensional array of numbers which has the largest sum. For example, for the sequence of values −2, 1, −3, 4, −1, 2, 1, −5, 4. The contiguous subarray with the largest sum is 4, −1, 2, 1, with sum 6.
var a = [ -2, 1, -3, 4, -1, 2, 1, -5, 4 ],
now = 0,
prev = 0;
for ( var i = 0; i < a.length; i++ ) {
prev = Math.max( 0, prev + a[ i ] );
now = Math.max( prev, now );
}
console.log( now ); // 6
@Dammmien
Dammmien / flexbox.css
Created February 25, 2016 18:07
CSS flexbox
.container{
display: flex;
flex-direction: row | row-reverse | column | column-reverse;
flex-wrap: nowrap | wrap | wrap-reverse;
flex-flow: <‘flex-direction’> || <‘flex-wrap’>;
justify-content: flex-start | flex-end | center | space-between | space-around; // horizontally
align-items: flex-start | flex-end | center | baseline | stretch; // vertical-align
align-content: flex-start | flex-end | center | space-between | space-around | stretch; //multi line repartition
}
@Dammmien
Dammmien / wget.sh
Last active February 2, 2025 18:55
wget cheat sheet
# POST a JSON file and redirect output to stdout
wget -q -O - --header="Content-Type:application/json" --post-file=foo.json http://127.0.0.1
# Download a complete website
wget -m -r -linf -k -p -q -E -e robots=off http://127.0.0.1
# But it may be sufficient
wget -mpk http://127.0.0.1
# Download all images of a website
@Dammmien
Dammmien / mustache.md
Last active April 27, 2023 22:41
Mustache cheatsheet

Basic tag

  Hello {{name}} !!

Comments

 {{! This is a comment, and it won't be rendered }}
@Dammmien
Dammmien / files.sh
Created March 9, 2018 19:38
Find unused files in a project (SVG in this example)
for FILE in $(find . -name "*.svg"); do
grep -qR $(basename "$FILE") * || echo "you can remove $FILE"
done
@Dammmien
Dammmien / euclidean.js
Created March 10, 2018 14:03
Get euclidean distance between two points A and B
const getDistance = (A, B) => Math.sqrt( Math.pow( B.x - A.x, 2 ) + Math.pow( B.y - A.y, 2 ) );
@Dammmien
Dammmien / Factory.js
Last active March 23, 2018 23:06
JavaScript Factory design pattern
class Car {
constructor(size, price, maxSpeed) {
this.size = size;
this.price = price;
this.maxSpeed = maxSpeed;
}
}
class CarFactory {
@Dammmien
Dammmien / Builder.js
Last active July 7, 2018 23:14
JavaScript Builder design pattern
class MealBuilder {
init() {
this.meal = new Meal();
}
addMain(type) {
this.meal.main = new MainItem(type);
}
@Dammmien
Dammmien / Singleton.js
Created March 23, 2018 23:09
JavaScript Singleton design pattern
class Singleton {
constructor() {
if (typeof Singleton.instance === 'object') {
return Singleton.instance;
}
Singleton.instance = this;
return this;
@Dammmien
Dammmien / Prototype.js
Created March 24, 2018 00:09
JavaScript Prototype design pattern
class Sheep {
constructor(name, weight) {
Object.assign(this, { name, weight });
}
clone() {
return new Sheep(this.name, this.weight);
}