CSC 270
Homework 5
Assigned Oct 25, due Nov 8

This assignment is to be done in Ruby.

You are encouraged to do this in teams of two, both students working together on each function; if you do this, turn in one homework with both names on it.

For all functions, follow the design recipe: each function must have a contract, a good collection of test cases, and a function definition.

For testing, you might want to use this testing framework, written by Chris Dollard. Read the source code to learn how to use it.

In order to get your homeworks graded quickly, I won't actually grade every one of these problems, but rather a sample including problems of various levels of difficulty. The rest are practice. If there's one that you want to make sure I look at, call my attention to it when you turn in the homework.

Within each heading, the problems are arranged more or less in increasing order of difficulty. If one of them really stumps you, try going on to a different heading, and then come back to this problem.

    Defining methods and classes

  1. Define a class named Candidate analogous to the struct we defined in homework 2. The class should have a constructor, getters, and a to_s method.

  2. Define a method named add-a-vote in this class analogous to the function we defined in homework 2. Note that the function should take no arguments (operating on self instead), and should return a new Candidate rather than modifying self.

  3. Working with arrays

    Note: The Array class has a lot of built-in methods which will make your life easier. See the Array documentation.

  4. Re-write the add-up function from homework 2 as a Ruby function named sum, taking in a Ruby array of numbers as its argument. It should produce an error message if any of the elements aren't numbers.

  5. Re-write sum as a method of the built-in Array class; it should take in no arguments, but rather add up the elements of self. It should produce an error message if any of the elements aren't numbers.

  6. Re-write the substitute function from homework 2 as a Ruby function that takes in two strings (old and new) and an array of strings, and returns an array of strings. This should be easy to do using a block iterator.

  7. Re-write the winner function from homework 2 as a Ruby function that takes in an array of Candidate objects. (You may want to use block iterators for this.)

  8. Re-write the scramble function from homework 3 as a Ruby function that takes in an array. Hint: It's a one-liner.

  9. Type hierarchies

  10. Re-do the "river" exercises from homework 2, with Ruby classes instead of Racket structs, and Ruby methods instead of Racket functions.

  11. Using and writing higher-order functions

  12. Rewrite the subsets function of homework 3 in Ruby. It should take in an array (of strings, numbers, symbols, it doesn't matter) and produce an array of arrays: all the possible subsets of the given array (with the elements in the same order they were in the given array). You may assume that the given array has no duplicate elements. For example,

    assert subsets([1,2,3]), [[],[3],[2],[2,3],[1],[1,3],[1,2],[1,2,3]]
    Hint: You don't have to use iterators over blocks for this, but it might help.

  13. Writing higher-order functions

  14. Develop a function doTwice which takes in an argument and a block, and returns the result of calling the block on the result of calling the block on the argument. For example,

    doTwice(4) {|x| x*x}
    should return 256.

  15. Generalize doTwice to a function iterate that takes in a natural number n, an X, and a block with contract "X -> X", and returns the result of calling the block n successive times. For example,

    iterate(5,3) {|x| x*2}
    should return 96.


  16. Last modified:  Wed Oct 19 16:22:58 EDT 2011
    Stephen Bloch / sbloch@adelphi.edu