This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# put this in your .git/hooks directory and name it pre-commit | |
bundle exec cane |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
NewerOlder