Skip to content

Instantly share code, notes, and snippets.

@felipernb
felipernb / weird.py
Created January 28, 2011 12:22
Where's the reference to L?
def f(a, L=[]):
L.append(a)
return L
print f(1)
print f(2)
print f(3)
@felipernb
felipernb / gist:822579
Created February 11, 2011 16:18
Benchmark
mustang:~ fribeiro$ time `python -c "print(12345 ** 54321)" > /dev/null`
real 0m22.969s
user 0m22.531s
sys 0m0.059s
mustang:~ fribeiro$ time `ruby -e "puts 12345**54321" > /dev/null`
real 0m10.007s
user 0m9.943s
@felipernb
felipernb / about.md
Created August 11, 2011 14:24 — forked from jasonrudolph/about.md
Programming Achievements: How to Level Up as a Developer
@felipernb
felipernb / stringify.js
Created August 16, 2011 08:38
Merges an array recursively and join it as a string with the specified glue character
var stringify = function(arr, glue) {
if (glue == undefined) glue = ',';
var seq = [];
var merge_recursive = function(a) {
// Using BFS algorithm as if it was a tree
for (var i = 0; i < a.length; i++) {
if (Array.isArray(a[i])) {
merge_recursive(a[i]);
} else {
seq.push(a[i]);
@felipernb
felipernb / max_vowel_counter.js
Created August 16, 2011 09:18
Given an array, normalize it to remove recursive arrays and return the number of distinct vowel in the word that has more distinct vowels
var merge_recursive = function(a, res) {
if (!Array.isArray(res)) res = [];
// Using BFS algorithm as if it was a tree
for (var i = 0; i < a.length; i++) {
if (Array.isArray(a[i])) {
merge_recursive(a[i], res);
} else {
res.push(a[i]);
}
}
@felipernb
felipernb / fib.pl
Created August 5, 2012 22:42
fibonacci in perl
#!/usr/bin/perl
# O(n) in time, O(1) in space
sub fib {
my $n = $_[0];
my $a = 0;
my $b = 1;
for ($i = 0; $i < $n; $i++) {
($a, $b) = ($a+$b, $a);
#problem specification: http://acm.uva.es/p/v1/105.html
buildings = [(1,11,5), (2,6,7), (3,13,9), (12,7,16), (14,3,25), (19,18,22), (23,13,29), (24,4,28)]
def skyline(buildings):
#initialize the skyline, setting 0 in all points
line = [0 for i in xrange(max(b[2] for b in buildings)+1)]
for b in buildings:
for x in xrange(b[0],b[2]):
line[x] = max(line[x], b[1])
skyline = []
@felipernb
felipernb / InsertionSort.java
Created August 9, 2012 19:44
Insertion Sort in Java
package com.feliperibeiro.kata;
public class InsertionSort {
/**
* In-place Insertion Sort - O(n^2)
*/
public void sort(int[] items) {
for (int i = 1; i < items.length; i++) {
int n = items[i];
@felipernb
felipernb / quicksort.rb
Created August 10, 2012 08:50
QuickSort in Ruby
#In-place QuickSort
def quicksort(a, l = 0, r = a.length)
return a if l >= r
p = partition(a,l,r)
quicksort(a, l, p)
quicksort(a, p+1, r)
a
end
def partition(a, l, r)
@felipernb
felipernb / fib.c
Created August 12, 2012 09:20
Fibonacci in C
//O(n) in time, O(1) in space
int fib(int n) {
int a, b, i, temp;
a = 0;
b = 1;
for (i = 0; i < n; i++) {
temp = a;
a = a + b;
b = temp;
}