Skip to content

Instantly share code, notes, and snippets.

View alexeykomov's full-sized avatar

Alexey alexeykomov

View GitHub Profile
@alexeykomov
alexeykomov / fibonacci-gen.js
Last active August 29, 2016 09:40
Fibonacci numbers via generator in JavaScript.
function* fibonacci() {
var earlierPrev = 0;
var prev = 0;
var index = -1;
while (true) {
index++;
if (index == 0) {
yield 0;
} else if (index == 1) {
prev = 1;
@alexeykomov
alexeykomov / fibonacci-recursive.js
Created December 10, 2015 14:08
Fibonacci number generation with recursive function.
function fibonacci(index) {
if (index == 0) {
return 0;
} else if (index == 1) {
return 1;
} else {
return fibonacci(index - 2) +
fibonacci(index - 1)
}
}
rflect.ui.MomentumScroller.prototype.doMomentum = function() {
// Calculate the movement properties. Implement getEndVelocity using the
// start and end position / time.
var velocity = this.getEndVelocity();
if (velocity != 0) {
var acceleration = this.getAcceleration(velocity);
var displacement = - (velocity * velocity) / (2 * acceleration);
var time = - velocity / acceleration;
var newY = this.contentOffsetY + displacement;
rflect.ui.MomentumScroller.prototype.snapToBounds = function() {
const transition = rflect.browser.css.getSelectorCasedProperty('transform') +
' ' + 500 + 'ms ease-out';
this.setTransitionAll(transition);
// Different out of bounds cases:
// 1. If content is lower than frame upper border
if (this.contentOffsetY > 0) {
this.contentOffsetY = 0;
rflect.browser.css.setTransform(this.getScrollBarContainer(),
function bubbleSort(aInput) {
const output = [...aInput];
for (let padding = 0; padding < output.length; padding++) {
for (let counter = 1; counter < output.length - padding; counter++){
const prev = output[counter - 1];
const next = output[counter];
if (prev > next) {
swap(output, counter - 1, counter);
}
}
function selectionSort(aInput) {
const output = [...aInput];
for (let padding = 0; padding < output.length; padding++) {
let minimal = output[padding];
let minimalIndex = padding;
for (let counter = padding + 1; counter < output.length; counter++) {
if (output[counter] < minimal) {
minimal = output[counter];
minimalIndex = counter;
}
function insertionSort(aInput) {
const output = [...aInput];
let lastSortedIndex = 0;
for (let counter = 1; counter < output.length; counter++) {
const itemInQuestion = output[counter];
let newIndexToPlace = counter;
for (let sortedIndex = counter - 1; sortedIndex >= 0; sortedIndex--) {
if (itemInQuestion < output[sortedIndex]) {
output[sortedIndex + 1] = output[sortedIndex];
function mergeSort(aInput) {
if (aInput.length == 0 || aInput.length == 1) {
return [...aInput];
} else {
const middleIndex = Math.floor(aInput.length / 2);
return merge(mergeSort(aInput.slice(0, middleIndex)),
mergeSort(aInput.slice(middleIndex)));
}
}
function quickSort(aInput, aLeftIndex, aRightIndex) {
if (aInput.length > 1) {
const delimeter = partition(aInput, aLeftIndex, aRightIndex);
if (aLeftIndex < delimeter - 1) {
quickSort(aInput, aLeftIndex, delimeter - 1);
}
if (delimeter < aRightIndex) {
quickSort(aInput, delimeter, aRightIndex);
}
}
const Token = {
NUMBER: 1,
LEFT_PAREN: 2,
RIGHT_PAREN: 3,
PLUS: 4,
MINUS: 5,
DIVIDE: 6,
MULTIPLY: 7
}