Skip to content

Instantly share code, notes, and snippets.

def factorial(number)
total = 1
for i in 1..number
total = total * i
end
return total
end
def sum_digits(number)
string = number.to_s
@albertein
albertein / smart_nl2br.php
Created January 26, 2012 16:42
Apply nl2br to a string except for the content of a given string (Useful for <pre> tags)
<?
function smart_nl2br($data, $opening_tag, $closing_tag) {
$results = "";
$closing_tag_length = strlen($closing_tag);
while(($position_start = strpos($data, $opening_tag)) !== false) {
$position_end = strpos($data, $closing_tag);
$previous_data = substr($data, 0, $position_start);
$tag_data = substr($data, $position_start, $position_end - $position_start + $closing_tag_length);
$data = substr($data, $position_end + $closing_tag_length);
$results = $results . nl2br($previous_data) . $tag_data;
@albertein
albertein / gist:1352164
Created November 9, 2011 17:23
Simple jQuery Ajax example.
<!DOCTYPE html>
<html>
<head>
<title>DEMO</title>
<script src="urlToJQuery"></script>
<script>
$(document).ready(function() {
$("#sumbit-button").click(function(){
var name = $("#name").val();
var age = $("#age").val();
@albertein
albertein / pixelate.rb
Created June 27, 2011 18:35
CodeBrawl pixelating contest entry
require 'chunky_png'
image = ChunkyPNG::Image.from_file('input.png')
width = image.width
height = image.height
size_factor = 10
pixelated_width = width / size_factor
pixelated_height = height / size_factor
@albertein
albertein / gist:744285
Created December 17, 2010 00:32
Using reflection + delegates to dinamically filter IEnumerable<T>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
namespace ConsoleApplication2
{
class Program
{
public class Empleado {
public string Nombre { get; set; }
public string ApellidoPaterno { get; set; }
public string ApellidoMaterno { get; set; }
}
public class App {
public static void Main() {
var empleados = new List<Empleado>();
//Se llena la coleccion de empleados, la implementación se deja como ejercicio al lector