From: Matthew Moss Date: 2008-06-28T00:56:57+09:00 Subject: [QUIZ] Statistician I (#167) ------=_Part_15850_27844130.1214582356754 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- The three rules of Ruby Quiz 2: 1. Please do not post any solutions or spoiler discussion for this quiz until 48 hours have passed from the time on this message. 2. Support Ruby Quiz 2 by submitting ideas as often as you can! (A permanent, new website is in the works for Ruby Quiz 2. Until then, please visit the temporary website at . 3. Enjoy! Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone on Ruby Talk follow the discussion. Please reply to the original quiz message, if you can. -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- ## Statistician I (#167) This week begins a three-part quiz, the final goal to provide a little library for parsing and analyzing line-based data. Hopefully, each portion of the larger problem is interesting enough on its own, without being too difficult to attempt. The first part -- this week's quiz -- will focus on the pattern matching. Let's look at a bit of example input: You wound Perl for 15 points of Readability damage. You wound Perl with Metaprogramming for 23 points of Usability damage. Your mighty blow defeated Perl. C++ walks into the arena. C++ wounds you with Compiled Code for 37 points of Speed damage. You wound C++ for 52 points of Usability damage. Okay, it's silly, but it is similar to a much larger data file I'll provide end for testing. You should definitely note the repetitiveness: just the sort of thing that we can automate. In fact, I've examined the input above and created three rules (a.k.a. patterns) that match (most of) the data: [The ] wounds you[ with ] for point[s] of [ damage]. You wound[ the] [ with ] for point[s] of [ damage]. Your mighty blow defeated[ the] . There are a few guidelines about these rules: 1. Text contained within square brackets is optional. 2. A word contained in angle brackets represents a field; not a literal match, but data to be remembered. 3. Fields are valid within optional portions. 4. You may assume that both the rules and the input lines are stripped of excess whitespace on both ends. Assuming the rules are in `rules.txt` and the input is in `data.txt`, running your Ruby script as such: > ruby reporter.rb rules.txt data.txt Should generate the following output: Rule 1: Perl, 15, Readability Rule 1: Perl, Metaprogramming, 23, Usability Rule 2: Perl # No Match Rule 0: C++, Compiled Code, 37, Speed Rule 1: C++, 52, Usability Unmatched input: C++ walks into the arena. Each line of the output corresponds to a line of the input; it indicates which rule was matched (zero-based index), and outputs the matched fields' values. Any lines of the input that could not be matched to one of the rules should output an "No Match" comment, with all the unmatched input records printed in the "Unmatched input" section at the end (so the author of the rules can extend them appropriately). One thing you should keep in mind while working on this week's quiz is that you want to be flexible; followup quizzes will require that you modify things a bit. For testing, I am providing two larger datasets: combat logs taken from Lord of the Rings Online gameplay. There is data for a [Guardian][1] and a [Hunter][2]; unzip before use. Both use the same ruleset: [The ] wounds you[ with ] for point[s] of [ damage]. You are wounded for point[s] of damage. You wound[ the] [ with ] for point[s] of [ damage]. You reflect point[s] of damage to[ the] . You succumb to your wounds. Your mighty blow defeated[ the] . [1]: http://www.splatbang.com/rubyquiz/files/guardian.zip [2]: http://www.splatbang.com/rubyquiz/files/hunter.zip -- Matthew Moss ------=_Part_15850_27844130.1214582356754--