M
Matthew Moss
[Note: parts of this message were removed to make it a legal post.]
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
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
<http://splatbang.com/rubyquiz/>.
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 ]<name> wounds you[ with <attack>] for <amount> point of <kind>[
damage].
You wound[ the] <name>[ with <attack>] for <amount> point of <kind>[
damage].
Your mighty blow defeated[ the] <name>.
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:
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 ]<name> wounds you[ with <attack>] for <amount> point of <kind>[
damage].
You are wounded for <amount> point of <kind> damage.
You wound[ the] <name>[ with <attack>] for <amount> point of <kind>[
damage].
You reflect <amount> point of <kind> damage to[ the] <name>.
You succumb to your wounds.
Your mighty blow defeated[ the] <name>.
[1]: http://www.splatbang.com/rubyquiz/files/guardian.zip
[2]: http://www.splatbang.com/rubyquiz/files/hunter.zip
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
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
<http://splatbang.com/rubyquiz/>.
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 ]<name> wounds you[ with <attack>] for <amount> point
damage].
You wound[ the] <name>[ with <attack>] for <amount> point
damage].
Your mighty blow defeated[ the] <name>.
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 ]<name> wounds you[ with <attack>] for <amount> point
damage].
You are wounded for <amount> point
You wound[ the] <name>[ with <attack>] for <amount> point
damage].
You reflect <amount> point
You succumb to your wounds.
Your mighty blow defeated[ the] <name>.
[1]: http://www.splatbang.com/rubyquiz/files/guardian.zip
[2]: http://www.splatbang.com/rubyquiz/files/hunter.zip