1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
#! /usr/bin/ruby
def countsmaller(nums)
b = nums.sort()
sm = Hash.new
l = 0
b.each_with_index do |e, i|
if i == 0 || e != l then
sm[e] = i
l = e
end
end
return nums.map {|n| sm[n]}
end
require 'test/unit'
class TestCountsmaller < Test::Unit::TestCase
def test_ex1
assert_equal([4, 0, 1, 1, 3], countsmaller([8, 1, 2, 2, 3]))
end
def test_ex2
assert_equal([2, 1, 0, 3], countsmaller([6, 5, 4, 8]))
end
def test_ex3
assert_equal([0, 0, 0], countsmaller([2, 2, 2]))
end
end
|