- Read Tutorial
- Watch Guide Video
- Complete the Exercise
Summary
Build out a method that sums the total number of lines of each character in Macbeth.
Credit to Upcase for the great coding exercise.
Exercise File
Exercise Description
Implement a method that parses Shakespeare's Macbeth play (stored in XML). The method should return a hash, with the key being the name of the character and the value being the total number of lines the character had.
If you fork the coding exercise repo you can access the XML data in the support
directory.
For reference, here is the XML source data.
Example
macbeth_counter['ROSS'] # => 135 macbeth_counter['FLEANCE'] # => 2 macbeth_counter # Full hash return value { "First Witch"=>62, "Second Witch"=>27, "Third Witch"=>27, "ALL"=>24, "DUNCAN"=>70, "MALCOLM"=>212, "Sergeant"=>35, "LENNOX"=>73, "ROSS"=>135, "MACBETH"=>718, "BANQUO"=>113, "ANGUS"=>21, "LADY MACBETH"=>265, "Messenger"=>23, "FLEANCE"=>2, "Porter"=>46, "MACDUFF"=>180, ["MACBETH", "LENNOX"]=>1, "DONALBAIN"=>10, "Old Man"=>11, "ATTENDANT"=>1, "First Murderer"=>30, "Second Murderer"=>15, "Both Murderers"=>2, "Servant"=>5, "Third Murderer"=>8, "Lords"=>3, "HECATE"=>39, "Lord"=>21, "First Apparition"=>2, "Second Apparition"=>4, "Third Apparition"=>5, "LADY MACDUFF"=>41, "Son"=>20, "Doctor"=>45, "Gentlewoman"=>23, "MENTEITH"=>12, "CAITHNESS"=>11, "SEYTON"=>5, "SIWARD"=>30, "Soldiers"=>1, "YOUNG SIWARD"=>7 }
Real World Usage
If you struggle with this exercise, don't be discouraged. This is not a trivial coding exercise. There are a number of ways to implement this solution, and in fact in the next guide I'm going to show an alternative solution that refactors the solution and utilizes a functional approach. To properly build this program you will need to be able to:
- Parse XML data
- Convert XML data into a hash data structure
- Worked with multiple nested iterators
- Perform calculations on dynamic data
Solution
Can be found on the solutions branch on github.