Skip to content

Instantly share code, notes, and snippets.

@VatslavDS
Created May 31, 2014 16:41
Show Gist options
  • Save VatslavDS/534b72c2bad37631d4cc to your computer and use it in GitHub Desktop.
Save VatslavDS/534b72c2bad37631d4cc to your computer and use it in GitHub Desktop.
/*var sys = require("sys");
var stdin = process.openStdin();
stdin.addListener("data", function(d){
console.log("You entered " + d.toString());
console.log(parseInt(d.toString()));
process.kill();
});
*/
//Algoritmo que identifica si un string tiene letras repetidas
var has_repetaed = function(string){
var current = string[0];
//var counter = 0;
for(var i = 0; i< string.length; i++){
if(current === string[i+1]){
console.log("Tiene letras repetidas salir");
}
}
};
//Algoritmo que cambia el orden de una cadena de caracteres
var swipe = function(string){
var new_string = "";
for(var i = string.length-1; i>= 0; i--){
new_string += string[i];
}
new_string += " i";
console.log(new_string.trim());
}
var bit_manipulation = function(){
var a = 11111111;
var b = 00000000;
console.log(a&b);
};
//Encontrar el maximo de de 3 numeros
var max_three = function(a, b, c){
if(a > b && b > c){
console.log("El mayor es a " + a);
}else if(b > a && a > c){
console.log("El mayor es b " +b);
}else if(c > b && b > a){
console.log("El mayor es c " + c);
}
};
//Binary Search
Array.prototype.binarySearchFast = function(search){
var size = this.length,
high = size - 1,
low = 0;
while(high > low){
if(this[low] === search) return low;
else if(this[high] === search) return high;
target = (((search - this[low]) / (this[high] - this[low])) * (high-low)) >>> 0;
if(this[target] === search) return target;
else if(search > this[target]) low = target + 1, high--;
else high = target -1, low++;
}
return -1;
};
var MergeSort = function(){
function sort(array){
var length = array.lengthl
mid = Math.floor(length * 0.5),
left = array.slice(0, mid),
right = array.slice(mid, length);
if(length === 1){
return array;
}
return merge(sort(left), sort(right));
}
function merge(left, right){
var result = [];
while(left.length || right.length){
if(left.length && right.length){
if(left[0] < right[0]){
result.push(left.shift());
}else{
result.push(right.shift());
}
}else if(left.length){
result.push(left.shift());
}else{
result.push(right.shift());
}
}
return result;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment