Skip to content

Instantly share code, notes, and snippets.

@albertein
albertein / highlight.js
Created July 5, 2012 00:48
Selection highlighting
function getSelectedText(){
if(window.getSelection){
return window.getSelection().toString();
}
else if(document.getSelection){
return document.getSelection();
}
else if(document.selection){
return document.selection.createRange().text;
}
@albertein
albertein / move.js
Last active November 28, 2022 11:42
Move an element frome an array up or down.
var move = function(array, element, delta) {
var index = array.indexOf(element);
var newIndex = index + delta;
if (newIndex < 0 || newIndex == array.length) return; //Already at the top or bottom.
var indexes = [index, newIndex].sort(); //Sort the indixes
array.splice(indexes[0], 2, array[indexes[1]], array[indexes[0]]); //Replace from lowest index, two elements, reverting the order
};
var moveUp = function(array, element) {
move(array, element, -1);
map = [
[8, 8, 4, 4, 5],
[4, 9, 6, 4, 8],
[8, 6, 4, 1, 2],
[4, 8, 2, 6, 3],
[0, 6, 8, 8, 4]
]
look = lambda { |position, visited, coins, path|
if visited.include? position
@albertein
albertein / factorial.rb
Created January 31, 2013 18:18
Recursive factorial
def factorial(n) # For n >= 1
return n if n == 1 #Base case, recursion ends here
return n * factorial(n - 1) #Recursive solution, multiply the current number by the factorial of the previous number
end
@albertein
albertein / binary_search.rb
Created January 31, 2013 18:23
Recursive binary search
def search(array, number) #array must be sorted
pivot = array.size / 2
return pivot if array[pivot] == number
if array[pivot] > number #Search on the left
return search array.slice 0, pivot, number
else #Search on the right
return search array.slice pivot, array.size - pivot
end
end
@albertein
albertein / dyntemplates.js
Created February 3, 2013 19:44
Cargar templates dinamicamente en angular.js
var app = angular.module('yourApp', []);
// No olvide inyectar el servicio $compile a su controlador
app.controller('yourCtrl', ['$scope', '$compile', function($scope, $compile) {
//Trae el template crudo del servidor
var template = "<div ng-repeat='item in items'>{{item.name}}</div>";
//Por simplicidad del ejemplo vamos a insertar el template con jquery
$('#sucontainer').html(template);
//Hasta este momento lo que inserto en el DOM es inerte,
//Angular no conoce de su existencia.
//Para lograrlo tenemos que compilar el template y
nodes = []
nodes.push(root)
while (!nodes.empty?) do
node = nodes.pop()
next if node.nil?
if (node.value < min)
node.parent.replace(node, node.right)
nodes.push(node.right)
elsif (node.value > max)
node.parent.replace(node, node.left)
def merge(l1, l2)
out = []
while(!(l1.empty? && l2.empty?)) do
list = nil
if (l2.empty? || l1[0].timestamp < l2[0].timestamp)
list = l1
else
list = l2
end
element = list.slice!(0)
@albertein
albertein / scrolling.js
Created May 6, 2014 16:07
Scroll cycle.
var ids = [“tab1”, “tab2”, “tab3”….., “tabn”];
var tabIndex = 0;
window.setInterval(function() {
tabIndex = (tabIndex + 1) % ids.length;
var element = document.getElementById(ids[tabIndex]);
element.scrollIntoView(true);
}, 15 * 1000);

Keybase proof

I hereby claim:

  • I am albertein on github.
  • I am albertein (https://keybase.io/albertein) on keybase.
  • I have a public key ASBQdWQFTBgaWxnJ2WYBtV3XuFO9pAozxjSvhF8K_34Szwo

To claim this, I am signing this object: