The goal of this multi-part assignment is to get you accustomed to basic Ruby coding and introduce you to RSpec, the unit testing tool we will be using heavily.
While we provide an explanation of how your code should work in this handout, you should get accustomed to the idea that the true specification is in the test files!
Therefore, we suggest you work on this assignment using autotest
,
which automatically re-runs all the RSpec tests each time you make
changes to your code:
-
In a terminal window, change to the root directory of this homework (the one containing subdirectories
lib/
andspec/
) and run the commandautotest
. RSpec expects to find code files underlib/
and the corresponding spec files underspec/
. -
Initially, all tests are marked "pending", as indicated by the argument
:pending => true
in eachdescribe
block. To start working on a question, remove this option:
e.g. in fun_with_strings_spec.rb
, change:
describe 'palindrome detection', :pending => true do
to
describe 'palindrome detection' do
and save the spec file. autotest
will
detect the change and automatically re-run the tests in that group, which
will now fail (displayed in red) since you haven't written any code yet.
- As you fill in code in the appropriate files under
lib/
, each time you save changes to that file the tests will automatically be re-run. When a test passes, it's displayed in green. Your goal is to get all tests for all parts to pass green.
(Note: when you submit your assignment, we may also run additional test cases beyond the ones given here.)
Specs: spec/fun_with_strings_spec.rb
Skeleton: lib/fun_with_strings.rb
In this problem, you'll implement three functions that perform
basic string processing. You can start from the template
fun_with_strings.rb
.
A palindrome is a word or phrase that reads the same forwards as backwards, ignoring case, punctuation, and nonword characters. (A "nonword character" is defined for our purposes as "a character that Ruby regular expressions would treat as a nonword character".)
You will write a method palindrome?
that returns true iff its
receiver is a palindrome.
As you can see in the template fun_with_strings.rb
, we arrange to mix
your method into the String
class so it can be called like this:
"redivider".palindrome? # => should return true
"adam".palindrome? # => should return false or nil
Your solution shouldn't use loops or iteration of any kind. Instead, you
will find regular-expression syntax very useful; it's reviewed briefly
in the book, and the Rubular website
lets you try out Ruby
regular expressions "live". Some methods that you might find useful
(which you'll have to look up in the Ruby
documentation) include
String#downcase
, String#gsub
, and String#reverse
.
The spec file contains a number of test cases. At a minimum, all should pass before you submit your code. We may run additional cases as well.
Define a function count_words
that, given an input string, return a
hash whose keys are words in the string and whose values are the number
of times each word appears:
"To be or not to be" # => {"to"=>2, "be"=>2, "or"=>1, "not"=>1}
Your solution shouldn't use for-loops, but iterators like each
are
permitted. As before, nonwords and case should be ignored. A word is
defined as a string of characters between word boundaries.
An anagram group is a group of words such that any one can be converted into any other just by rearranging the letters. For example, "rats", "tars" and "star" are an anagram group.
Given a space separated list of words in a single string, write a method that groups them into anagram groups and returns a nested array of those groups (an array of arrays). Case doesn't matter in classifying string as anagrams (but case should be preserved in the output), and the order of the anagrams in the groups doesn't matter.
Specs: spec/dessert_spec.rb
Skeleton: lib/dessert.rb
-
Create a class Dessert with getters and setters for name and calories. The constructor should accept arguments for name and calories.
-
Define instance methods
healthy?
, which returns true iff a dessert has less than 200 calories, anddelicious?
, which returns true for all desserts. -
Create a class JellyBean that inherits from Dessert. The constructor should accept a single argument giving the jelly bean's flavor; a newly-created jelly bean should have 5 calories and its name should be the flavor plus "jelly bean", for example, "strawberry jelly bean".
-
Add a getter and setter for the flavor.
-
Modify
delicious?
to return false if the flavor islicorice
, but true for all other flavors. The behavior ofdelicious?
for non-jelly-bean desserts should be unchanged.
Specs: spec/rock_paper_scissors_spec.rb
Skeleton: lib/rock_paper_scissors.rb
In a game of rock-paper-scissors, each player chooses to play Rock (R), Paper (P), or Scissors (S). The rules are: Rock breaks Scissors, Scissors cuts Paper, but Paper covers Rock.
In a round of rock-paper-scissors, each player's name and strategy is encoded as an array of two elements
[ "Armando", "P" ] # => Armando plays Paper
[ "Dave", "S" ] # => Dave plays Scissors
(In this example, Dave would win because Scissors cuts Paper.)
Create a RockPaperScissors
class with a class method winner
that
takes two 2-element arrays like those above, and returns the one
representing the winner:
RockPaperScissors.winner(['Armando','P'], ['Dave','S'])
# => ['Dave','S']
If either player's strategy is something other than "R", "P" or "S"
(case-SENSITIVE), the method should raise a
RockPaperScissors::NoSuchStrategyError
exception and provide the message:
"Strategy must be one of R,P,S".
If both players use the same strategy, the first player is the winner.
A rock-paper-scissors tournament is encoded as an array of games - that is, each element can be considered its own tournament.
[
[
[ ["Armando", "P"], ["Dave", "S"] ],
[ ["Richard", "R"], ["Michael", "S"] ]
],
[
[ ["Allen", "S"], ["Omer", "P"] ],
[ ["David E.", "R"], ["Richard X.", "P"] ]
]
]
Under this scenario, Dave would beat Armando (S>P) and Richard would beat Michael (R>S), so Dave and Richard would play (Richard wins since R>S); similarly, Allen would beat Omer, David E. would beat Richard X., and Allen and Richard X. would play (Allen wins since S>P); and finally Richard would beat Allen since R>P. That is, pairwise play continues until there is only a single winner.
Write a method `RockPaperScissors.tournament_winner' that takes a tournament encoded as an array and returns the winner (for the above example, it should return ['Richard', 'R']). You can assume that the array is well formed (that is, there are 2^n players, and each one participates in exactly one match per round).
HINT: Formulate the problem as a recursive one whose base case you solved in part 1.
Specs: spec/attr_accessor_with_history_spec.rb
Skeleton: lib/attr_accessor_with_history.rb
In lecture we saw how attr_accessor
uses metaprogramming to create
getters and setters for object attributes on the fly.
Define a method attr_accessor_with_history
that provides the same
functionality as attr accessor but also tracks every value the attribute
has ever had:
class Foo
attr_accessor_with_history :bar
end
f = Foo.new # => #<Foo:0x127e678>
f.bar = 3 # => 3
f.bar = :wowzo # => :wowzo
f.bar = 'boo!' # => 'boo!'
f.bar_history # => [nil, 3, :wowzo]
(Calling bar_history
before bar
's setter is ever called should
return nil
.)
History of instance variables should be maintained separately for each object instance. that is:
f = Foo.new
f.bar = 1 ; f.bar = 2
g = Foo.new
g.bar = 3 ; g.bar = 4
g.bar_history
then the last line should just return [nil,3]
, rather than
[nil,1,2,3]
.
If you're interested in how the template works,
the first thing to notice is that if we define
attr_accessor_with_history
in class Class
, we can use it as in the
snippet
above. This is because a Ruby class like Foo
or String
is actually just an
object of class Class
. (If that makes your brain hurt, just don't worry
about it for now. It'll come.)
The second thing to notice is that Ruby
provides a method class_eval
that takes a string and evaluates it in the
context of the current class, that is, the class from which you're
calling attr_accessor_with_history
. This string will need to contain a
method definition that implements a setter-with-history for the desired
attribute attr_name
.
HINTS:
- Don't forget that the very first time the attribute receives a value, its history array will have to be initialized.
- An attribute's initial value is always
nil
by default, so iffoo_history
is referenced beforefoo
has ever been assigned, the correct answer isnil
, but after the first assignment tofoo
, the correct value forfoo_history
would be[nil]
. - Don't forget that instance variables are referred to as
@bar
within getters and setters, as Section 3.4 of ELLS explains. - Although the existing
attr_accessor
can handle multiple arguments (e.g.attr_accessor :foo, :bar
), your version just needs to handle a single argument. - Your implementation should be general enough to work in the context of any class and for attributes of any (legal) variable name.
- Note that one powerful metaprogramming feature in Ruby is
class_eval
that can be called in the meta-classClass
.class_eval
can interpret a string on the fly to create some new code. In the example below, we defineadd_method()
in the meta-class (and, through inheritance, available to any class). When called, this method defines a new method that returns 42 (notice how#{name}
gets replaced with the parameter passed toadd_method
).
class Class
def add_method (name)
class_eval %Q{
def #{name}()
42
end
}
end
end
class MyClass
add_method :my_method
end
mc = MyClass.new
puts mc.my_method # => 42
Skeleton: lib/cartesian_product.rb
Given two collections (of possibly different lengths), we want to get the Cartesian product of the sequences—in other words, every possible pair of N elements where one element is drawn from each collection.
For example, the Cartesian product of the sequences a==[:a,:b,:c]
and b==[4,5]
is:
a×b == [[:a,4],[:a,5],[:b,4],[:b,5],[:c,4],[:c,5]]
Create a method that accepts two sequences and returns an iterator that will yield the elements of the Cartesian product, one at a time, as a two-element array.
- It doesn't matter what order the elements are returned in. So for the above example,
the ordering
[[:a,4], [:b,4], [:c,4], [:a,5], [:b,5], [:c,5]]
would be correct, as would any other ordering. - It does matter that within each pair, the order of the elements matches the order in
which the original sequences were provided. That is,
[:a,4]
is a member of the Cartesian product a×b, but[4,:a]
is not. (Although[4,:a]
is a member of the Cartesian product b×a.)
Here's some sample code showing how your iterator could be used:
class CartesianProduct
include Enumerable
# your code here
end
c = CartesianProduct.new([:a,:b], [4,5])
c.each { |elt| puts elt.inspect }
# [:a, 4]
# [:a, 5]
# [:b, 4]
# [:b, 5]
c = CartesianProduct.new([:a,:b], [])
c.each { |elt| puts elt.inspect }
# (nothing printed since Cartesian product
# of anything with an empty collection is empty)