-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbasic.rb
More file actions
57 lines (45 loc) · 1.32 KB
/
basic.rb
File metadata and controls
57 lines (45 loc) · 1.32 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
require_relative './common.rb'
def solution
map = {}
File.foreach(MEASUREMENTS_PATH) do |line|
parts = line.split(";")
city_name = parts[0]
measurement = parts[1].to_i
if map[city_name]
map[city_name] = {
min: [map[city_name][:min], measurement].min,
max: [map[city_name][:max], measurement].max,
total: map[city_name][:total] + measurement,
count: map[city_name][:count] + 1,
}
else
map[city_name] = {
min: measurement,
max: measurement,
total: measurement,
count: 1,
}
end
end
sorted_map = map.sort.to_h
bucket = ""
sorted_map.each do |city_name, status|
avg = status[:total] / status[:count]
line = "#{city_name}=#{status[:min]};#{status[:max]};#{avg}(#{status[:total]}/#{status[:count]})\n"
bucket += line
end
bucket
end
def main
timer = Timer.new
expect_output = File.read(OUTPUT_PATH)
got = solution
if got == expect_output
puts "Test Passed"
elsif
puts "Test Failed\nExpected: #{expect_output}\nGot: #{got}"
end
elapsed = timer.elapsed_as_milliseconds
puts "Elapsed: #{elapsed} ms"
end
main