Skip to content

Instantly share code, notes, and snippets.

@felipernb
felipernb / roman_to_decimal.go
Created September 19, 2012 13:20
Roman to decimal, in GO
package roman_to_decimal
func RomanToDecimal(roman string) int {
table := map[uint8] int {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
@felipernb
felipernb / insertion_sort.rb
Created September 14, 2012 08:52
Insertion Sort in Ruby
def insertion_sort(a)
for i in 1..a.length-1 do
n = a[i]
pos = i
while pos > 0 and a[pos-1] > n do
a[pos] = a[pos-1]
pos -= 1
end
a[pos] = n
end
@felipernb
felipernb / insertion_sort.py
Created September 11, 2012 14:20
Insertion Sort in Python
def insertion_sort(a):
for i in xrange(1, len(a)):
n = a[i]
pos = i
while pos > 0 and a[pos-1] > n:
a[pos] = a[pos-1]
pos -= 1
a[pos] = n
return a
package quicksort
func Quicksort(a []int) []int {
if (len(a) <= 1) {
return a
}
p := partition(a)
Quicksort(a[0:p])
Quicksort(a[p+1:])
return a
#!/usr/bin/perl
sub factorial {
my $n = $_[0];
if ($n <= 0) {
return 1;
}
return $n * factorial($n-1)
}
@felipernb
felipernb / randomized_selection.go
Created August 26, 2012 22:18
Randomized selection in Go
package randomselect
import (
"math/rand"
)
func RSelect(a []int, i int) int {
n := len(a)
if n == 1 {
return a[0]
@felipernb
felipernb / MaximumSubarray.java
Created August 21, 2012 07:13
Maximum Subarray
package com.feliperibeiro;
import java.util.Arrays;
public class MaximumSubarray {
public int[] max(int[] a) {
int maxSum = 0,
currentSum = 0,
maxSumStart = 0,
@felipernb
felipernb / huffman.go
Created August 17, 2012 19:32
Huffman in Go (probably incorrect)
package main
import (
"container/heap"
"fmt"
)
type Node struct {
value uint8
priority int
@felipernb
felipernb / bowling.go
Created August 13, 2012 18:39
The Bowling Game Kata
//bowling.go
package bowling
type Game struct {
rolls []int
currentRoll int
}
func NewGame() *Game {
return &Game{make([]int, 21), 0}
@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;
}