Skip to content

Instantly share code, notes, and snippets.

@britishtea
Last active December 20, 2015 01:09
Show Gist options
  • Save britishtea/6046736 to your computer and use it in GitHub Desktop.
Save britishtea/6046736 to your computer and use it in GitHub Desktop.
Memoized Fibonacci numbers with Hash default values
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
$ 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