Skip to content

Commit d042230

Browse files
committed
Rings and Disks aka Omega 2013
1 parent 0158c5e commit d042230

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

omega2013.rb

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# http://blog.codility.com/2013/07/omega-2013-codility-programming.html
2+
# rings and disks
3+
4+
def rings_and_disks(a, b)
5+
count = 0
6+
7+
# transform well
8+
a.each_index do |i|
9+
next if i == 0
10+
a[i] = a[i - 1] < a[i] ? a[i - 1] : a[i]
11+
end
12+
13+
# place disks
14+
b.each do |disk|
15+
while a.any?
16+
ring = a.pop
17+
if disk <= ring
18+
count += 1
19+
break
20+
end
21+
end
22+
end
23+
count
24+
end
25+
26+
require 'minitest/autorun'
27+
28+
class Tests < MiniTest::Unit::TestCase
29+
def test_example_input
30+
assert_equal 4, rings_and_disks([5, 6, 4, 3, 6, 2, 3], [2, 3, 5, 2, 4])
31+
end
32+
33+
def test_small_rings
34+
assert_equal 0, rings_and_disks([1, 2, 3], [5, 1, 1])
35+
end
36+
37+
def test_all_rings
38+
assert_equal 5, rings_and_disks([5, 4, 3, 2, 1], [1, 2, 3, 4, 5])
39+
end
40+
end

0 commit comments

Comments
 (0)