Skip to content

Instantly share code, notes, and snippets.

@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
@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 / 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
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 / 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);
@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 / transtatement.php
Created June 4, 2012 01:18
PDO transactions + prepared statemes
$dbh->beginTransaction();
$statement = $dbh->prepare("INSERT INTO TABLE (field1, field2) VALUES (:v1, :v2)");
$stmt->bindParam(':v1', $value1);
$stmt->bindParam(':v2', $value2);
for ($i = 0; $i < 300; $i++) {
$value1 = 'value';
$value2 = 'another value';
$stmt->execute();
}
@albertein
albertein / multi.vb
Created March 23, 2012 04:27
Primer programa de mi hijo
TextWindow.WriteLine("hola")
TextWindow.WriteLine(" hola soy tito ")
TextWindow.WriteLine( "te amo mama " )
TextWindow.Write("8x7=")
TextWindow.WriteLine(8*7)
TextWindow.WriteLine(10+20*53)
TextWindow.WriteLine("3 en ingles es tree")
@albertein
albertein / half_text.rb
Created February 10, 2012 19:03
Third challenge for http://apply.embed.ly
def count_words(max_occurrences, unique_words)
unique_words -= 1
total_words = max_occurrences
for i in 2..unique_words
total_words += max_occurrences / i.to_f
end
return total_words
end
def search(max_ocurrences, unique_words)
@albertein
albertein / standar_dev.rb
Created February 10, 2012 18:54
Second challenfe for http://apply.embed.ly
# encoding: UTF-8
def standar_deviation(values)
total = 0.0
values.each { |value| total += value }
average = total / values.length
total = 0.0
deviation = values.each { |value|
total += (value - average) ** 2
}