Created
April 16, 2013 01:10
-
-
Save Catharz/5392591 to your computer and use it in GitHub Desktop.
Codility Omega 2013 - Falling Disks
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
def falling_disks(rings, disks) | |
min, disk = rings[0], 0 | |
rings.each_index do |ring| | |
min = rings[ring] if rings[ring] < min | |
rings[ring] = min if rings[ring] > min | |
end | |
(rings.size - 1).downto(0) do |ring| | |
disk += 1 if disks[disk] <= rings[ring] | |
return disk if disk == disks.size | |
end | |
disk | |
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
require 'spec_helper' | |
require_relative '../falling_disks' | |
describe '#falling_disks' do | |
it 'passes the sanity check' do | |
rings, disks = [5, 6, 4, 3, 6, 2, 3], [2, 3, 5, 2, 4] | |
falling_disks(rings,disks).should == 4 | |
end | |
it 'drops disks to the bottom' do | |
rings, disks = [5, 5, 5], [4, 4, 4] | |
falling_disks(rings,disks).should == 3 | |
end | |
it 'only drops the number of rings that can fit' do | |
rings, disks = [5, 5, 5], [4, 4, 4, 4] | |
falling_disks(rings,disks).should == 3 | |
end | |
it 'only drops disks that can fit' do | |
rings, disks = [5, 3, 5], [4, 4, 4, 4] | |
falling_disks(rings,disks).should == 1 | |
end | |
it 'only drops as many disks as exist' do | |
rings, disks = [5, 5, 5, 5], [4, 4, 4] | |
falling_disks(rings,disks).should == 3 | |
end | |
it 'only drops the first ring when only the first will fit' do | |
rings, disks = [7, 6, 6, 6, 19, 7, 7, 6, 8, 13, 9, 12], [7, 3, 2, 5, 6] | |
falling_disks(rings,disks).should == 1 | |
end | |
it 'will not drop any disks when none will fit' do | |
rings, disks = [6, 15, 5, 17, 5, 20, 16, 17, 10, 13, 8, 10], [7, 4, 4, 3, 1] | |
falling_disks(rings,disks).should == 0 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment