Last active
December 20, 2015 01:09
-
-
Save britishtea/6046736 to your computer and use it in GitHub Desktop.
Memoized Fibonacci numbers with Hash default values
This file contains 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
Fib = Hash.new { |hash, key| hash[key] = hash[key - 2] + hash[key - 1] } | |
Fib.merge! 0 => 0, 1 => 1 | |
require 'minitest' | |
require 'minitest/autorun' | |
describe Fib do | |
it 'fibs' do | |
assert_equal Fib[0], 0 | |
assert_equal Fib[1], 1 | |
assert_equal Fib[2], 1 | |
assert_equal Fib[5], 5 | |
assert_equal Fib[10], 55 | |
assert_equal Fib[20], 6765 | |
end | |
end |
This file contains 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
$ ruby fib.rb | |
Run options: --seed 52550 | |
# Running: | |
. | |
Finished in 0.006770s, 147.7105 runs/s, 886.2629 assertions/s. | |
1 runs, 6 assertions, 0 failures, 0 errors, 0 skips |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment