-
Notifications
You must be signed in to change notification settings - Fork 0
/
2018-exercise-7.rb
58 lines (52 loc) · 1.55 KB
/
2018-exercise-7.rb
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
58
# Source: https://programmingpraxis.com/2018/06/01/exercise-7/
#
# Exercise 7 by programmingpraxis
# This must be somebody's homework:
#
# You are given an input file containing lines with three pipe-separated fields;
# the first field is a student number (a positive integer), the second field is
# a class name (a string), and the third field is the grade the student received
# for the class (a non-negative integer, no greater than 100):
#
# 22|Data Structures|45
# 23|English|52
# 22|English|51
# 26|Data Structures|72
# 23|Data Structures|61
# 21|English|81
#
# You are to output a list of class names along with the grade earned by the
# lowest-numbers student in each class. For instance, given the above data, the
# output for Data Structures is 45 corresponding to student 22 (with student
# number lower than 23 or 26, who also took Data Structures) and the output for
# English is 81 corresponding to student 21 (with student number lower than 22
# or 23, who also took English).
require 'csv'
def fn(csv)
CSV
.parse(csv, col_sep: '|')
.group_by { |_, c_name, _| c_name }
.map do |c_name, row|
[c_name, row.min_by { |s_num, _| s_num.to_i }.last]
end
end
# Test
def test(target, output, input)
puts
puts "target: #{target}"
puts "output: #{output}"
puts "pass: #{output == target ? '✅' : '❌'}"
end
scores = <<-SCORES
22|Data Structures|45
23|English|52
22|English|51
26|Data Structures|72
23|Data Structures|61
21|English|81
SCORES
target = [
['Data Structures', '45'],
['English', '81']
]
test target, fn(scores), scores