This gist is part of a blog post. Check it out at:
http://jasonrudolph.com/blog/2011/08/09/programming-achievements-how-to-level-up-as-a-developer
| def f(a, L=[]): | |
| L.append(a) | |
| return L | |
| print f(1) | |
| print f(2) | |
| print f(3) |
| 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 |
This gist is part of a blog post. Check it out at:
http://jasonrudolph.com/blog/2011/08/09/programming-achievements-how-to-level-up-as-a-developer
| 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]); |
| 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]); | |
| } | |
| } |
| #!/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 = [] |
| 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]; |
| #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) |
| //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; | |
| } |