Skip to content

Instantly share code, notes, and snippets.

View egermano's full-sized avatar
🏠
Working from home

Bruno Germano egermano

🏠
Working from home
View GitHub Profile
@egermano
egermano / bind.js
Created August 31, 2012 15:58
bind to change the function scope
// bind to change the function scope and send arguments for the functions
Function.prototype.bind = function(context) {
var __method = this,
args = Array.prototype.slice.call(arguments, 1);
return function() {
var a = Array.prototype.concat(Array.prototype.slice.call(arguments), args);
return __method.apply(context, Array.prototype.slice.call(a));
}
};
@egermano
egermano / gist:3556046
Created August 31, 2012 17:16
Fixing the “unix:///var/mysql/mysql.sock” not found error on MAMP
sudo mkdir /var/mysql
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock
@egermano
egermano / linear.search.js
Created September 11, 2012 14:59
Linear search
function linearSearch(values, target) {
for(var i = 0; i < values.length; ++i){
if (values[i] == target) { return i; }
}
return -1;
}
@egermano
egermano / array.list.js
Created September 11, 2012 15:02
Array List
function ArrayList(initialLength) {
this.length = 0;
this.array = new Array(initialLength);
this.add = function(value) {
if (this.length == this.array.length){
this.grow();
}
this.array[this.length++] = value;
};
@egermano
egermano / linked.list.js
Created September 11, 2012 15:03
Linked List
function LinkedList() {
this.head = null;
this.tail = null;
this.add = function(value) {
var node = new Node(value);
if (this.head == null) { this.head = node; }
if (this.tail != null) { this.tail.next = node; }
this.tail = node;
};
@egermano
egermano / hash.table.js
Created September 11, 2012 15:09
Hash Tables
function HashTable(size) {
this.size = size;
this.buckets = new Array(size);
this.add = function(value) {
var index = this.hash(value);
this.buckets[index] = value;
};
this.hash = function(value) {
var sum = 0;
@egermano
egermano / binary.search.js
Created September 11, 2012 15:14
Binary Search
function binSearch(values, target) {
return binarySearch(values, target, 0, values.length - 1);
};
function binarySearch(values, target, start, end) {
if (start > end) { return -1; } //does not exist
var middle = Math.floor((start + end) / 2);
var value = values[middle];
@egermano
egermano / bubble.sort.js
Created September 11, 2012 15:17
Bubble Sort
function bubbleSort(values) {
var length = values.length - 1;
do {
var swapped = false;
for(var i = 0; i < length; ++i) {
if (values[i] > values[i+1]) {
var temp = values[i];
values[i] = values[i+1];
values[i+1] = temp;
swapped = true;
@egermano
egermano / insertion.sort.js
Created September 11, 2012 17:48
Insertion Sort
function insertionSort(values) {
var length = values.length;
for(var i = 1; i < length; ++i) {
var temp = values[i];
var j = i - 1;
for(; j >= 0 && values[j] > temp; --j) {
values[j+1] = values[j];
}
values[j+1] = temp;
}
@egermano
egermano / gist:3745791
Created September 18, 2012 20:47
Get Day Delta
function getDayDelta(incomingYear,incomingMonth,incomingDay){
var incomingDate = new Date(incomingYear,incomingMonth-1,incomingDay),
today = new Date(), delta;
// EDIT: Set time portion of date to 0:00:00.000
// to match time portion of 'incomingDate'
today.setHours(0);
today.setMinutes(0);
today.setSeconds(0);
today.setMilliseconds(0);