Re: Ruby Quiz - Challenge #4 - Turn Humanitarian eXchange Language (HXL) Tabular Records into Named Tuples


(Frank J. Cameron) #1

Gerald Bauer wrote:

  Challenge #4 - Turn Humanitarian eXchange Language (HXL) Tabular
Records into Named Tuples

$ ruby ./lib/004.rb
Run options: --seed 28092
# Running:
.F
Finished in 0.005780s, 346.0336 runs/s, 346.0336 assertions/s.
  1) Failure:
RubyQuizTest#test_level2 [/home/fjc/ruby-quiz/004/test.rb:71]:
--- expected
+++ actual
@@ -1 +1 @@
-[{"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"}]
+[{"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", "Region 3"],
"adm1"=>nil}, {"sector+en"=>"WASH", "subsector"=>"Subsector 4",
"org"=>"Org 1", "country"=>"Country 3", "sex+targeted"=>["80", "95"],
"adm1"=>"Region 4"}]
2 runs, 2 assertions, 1 failures, 0 errors, 0 skips

$ cat ./lib/004.rb
require_relative '../004/test.rb'
class RubyQuizTest
  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
RubyQuizTest.new('fjc')


(Gerald Bauer) #2

Hello,
   Thanks for the first HXL parser solution (even with a handicap).
Sorry for the typo (fixed now) - changed "250,300" to "250","300" in
the expected records result in level 2.

   I've added your snippet to the solultion.rb [1] script:

  Finished in 0.007808s, 256.1529 runs/s, 256.1529 assertions/s.
2 runs, 2 assertions, 0 failures, 0 errors, 0 skips

     Have a great weekend. Cheers. Prost.

[1] https://github.com/planetruby/quiz/blob/master/004/solution.rb