-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsolution.rb
123 lines (99 loc) · 3.52 KB
/
solution.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
require 'minitest/autorun'
#
# An entry by Frank J. Cameron
# see https://rubytalk.org/t/re-ruby-quiz-challenge-4-turn-humanitarian-exchange-language-hxl-tabular-records-into-named-tuples/74829
module Frank
def parse(recs)
header_index = recs.find.with_index do |rec, i|
break i if rec.any?{|r| r[0] == ?# }
end
header_rec = recs[header_index].map do |r|
r.delete!(?#)
end
recs[header_index+1..-1].map do |rec|
{}.tap do |rec_hash|
header_rec.zip(rec) do |(h,r)|
next unless h
if rec_hash.has_key?(h)
if rec_hash[h].instance_of?(Array)
rec_hash[h] << r
else
rec_hash[h] = [rec_hash[h]] << r
end
else
rec_hash[h] = r
end
end
end
end
end
end # module Frank
#
# Test entry
# by Gerald Bauer
module TestAnswer
require 'csvhuman'
## todo: set (builtin/default) type converter to :none
def parse(recs) CsvHuman.parse( recs ); end
end # module TestAnswer
class RubyQuizTest < MiniTest::Test
def recs1
[["Organisation", "Cluster", "Province" ],
[ "#org", "#sector", "#adm1" ],
[ "Org A", "WASH", "Coastal Province" ],
[ "Org B", "Health", "Mountain Province" ],
[ "Org C", "Education", "Coastal Province" ],
[ "Org A", "WASH", "Plains Province" ]]
end
def recs1_expected
[{"org" => "Org A", "sector" => "WASH", "adm1" => "Coastal Province"},
{"org" => "Org B", "sector" => "Health", "adm1" => "Mountain Province"},
{"org" => "Org C", "sector" => "Education", "adm1" => "Coastal Province"},
{"org" => "Org A", "sector" => "WASH", "adm1" => "Plains Province"}]
end
def recs2
[["What","","","Who","Where","For whom",""],
["Record","Sector/Cluster","Subsector","Organisation","Country","Males","Females","Subregion"],
["","#sector+en","#subsector","#org","#country","#sex+#targeted","#sex+#targeted","#adm1"],
["001","WASH","Subsector 1","Org 1","Country 1","100","100","Region 1"],
["002","Health","Subsector 2","Org 2","Country 2","","","Region 2"],
["003","Education","Subsector 3","Org 3","Country 2","250","300","Region 3"],
["004","WASH","Subsector 4","Org 1","Country 3","80","95","Region 4"]]
end
def recs2_expected
[{"sector+en" => "WASH",
"subsector" => "Subsector 1",
"org" => "Org 1",
"country" => "Country 1",
"sex+targeted" => ["100", "100"],
"adm1" => "Region 1"},
{"sector+en" => "Health",
"subsector" => "Subsector 2",
"org" => "Org 2",
"country" => "Country 2",
"sex+targeted" => ["", ""],
"adm1" => "Region 2"},
{"sector+en" => "Education",
"subsector" => "Subsector 3",
"org" => "Org 3",
"country" => "Country 2",
"sex+targeted" => ["250", "300"],
"adm1" => "Region 3"},
{"sector+en" => "WASH",
"subsector" => "Subsector 4",
"org" => "Org 1",
"country" => "Country 3",
"sex+targeted" => ["80", "95"],
"adm1" => "Region 4"}]
end
end # class RubyQuizTest
class FrankTest < RubyQuizTest
include Frank
def test_level1() assert_equal recs1_expected, parse( recs1 ); end
def test_level2() assert_equal recs2_expected, parse( recs2 ); end
end
class TestAnswerTest < RubyQuizTest
include TestAnswer
def test_level1() assert_equal recs1_expected, parse( recs1 ); end
def test_level2() assert_equal recs2_expected, parse( recs2 ); end
end