Skip to content

Instantly share code, notes, and snippets.

@mikrofusion
mikrofusion / pre-commit
Created April 9, 2014 18:12
Add cane to git pre-commit hook
#!/bin/sh
# put this in your .git/hooks directory and name it pre-commit
bundle exec cane
@mikrofusion
mikrofusion / find_duplicates.rb
Created March 17, 2014 05:16
find duplicates (ruby)
#!/usr/bin/ruby
def find_duplicates(arr) # O(n)
hash = Hash.new(0)
duplicates = []
arr.each do |val|
if hash.has_key? val and hash[val] == 1 then duplicates.push val end
hash[val] += 1
end
@mikrofusion
mikrofusion / pi.rb
Created March 17, 2014 04:28
calculate pi
#!/usr/bin/ruby
def pi_recursive(depth, count, i)
if count >= depth then return 0 end
r = 4.0 / (i..(i + 2)).to_a.inject(1) { |sum, x| sum * x }
pi_recursive(depth, count + 1, i + 2) +
if count % 2 == 1 then + r else - r end
end
@mikrofusion
mikrofusion / reverse_array.rb
Created March 16, 2014 18:44
reverse array (ruby)
#!/usr/bin/ruby
class Array
def reverse_recursive(arr)
if arr.length == 1 then return arr end
a = arr.shift
reverse_recursive(arr) << a
end
@mikrofusion
mikrofusion / fibonacci.rb
Last active May 6, 2019 01:48
fibonacci (ruby)
#!/usr/bin/ruby
def fibonacci_recursive(limit, count, first, second)
if count == limit then return [first + second] end
[first + second].concat fibonacci_recursive(limit, count + 1, second, first + second)
end
def fibonacci(limit)
[1, 1].concat fibonacci_recursive(limit, 0, 1, 1)