CMSI 386
Homework #1

For the first homework assignment, you'll be warming up to Ruby. Readings: Read as many on-line tutorials or book chapters on Ruby as is reasonable without seriously impacting your personal life.

  1. Write a Ruby method that takes in a string s and returns the string which is equivalent to s but with all ASCII vowels removed. For example, in irb:
    >> strip_vowels("Hello, world")
    => "Hll, wrld"
    
  2. Write a Ruby method that randomly permutes a string. By random we mean that each time you call the method for a given argument all possible permutations are equally likely (note that "random" is not the same as "arbitrary.") For example, in irb:
    >> scramble("Hello, world")
    => "w,dlroH elol"
    
  3. Write a Ruby generator method that yields powers of two starting at 1 and going up to some limit. For example, in irb:
    >> powers_of_two(70) {|x| puts x}
    1
    2
    4
    8
    16
    32
    64
    => nil
    
  4. Write a Ruby generator method that yields powers of an arbitrary base starting at exponent 0 and going up to some limit. For example, in irb:
    >> powers(3, 400) {|x| puts x}
    1
    3
    9
    27
    81
    243
    => nil
    
  5. Write a method that interleaves two arrays. If the arrays do not have the same length, the elements of the longer array should end up at the end of the result array. For example, in irb:
  6. >> interleave(["a", "b"], [1, 2, true, nil])
    => ["a", 1, "b", 2, true, nil]
    
  7. Write a method that doubles up each item in an array. For example, in irb:
    >> [5,4,[3],9].stutter
    => [5,5,4,4,[3],[3],9,9]
    
  8. Write a Ruby script (in the file prefixes.rb) that writes successive prefixes of its first input argument, one per line, starting with the first prefix, which is zero characters long. For example:
    $ ruby prefixes.rb matsumoto
    
    m
    ma
    mat
    mats
    matsu
    matsum
    matsumo
    matsumot
    matsumoto
    
  9. Write a ruby script (in the file lines.rb) that reports the number of lines in the file named by the first argument. For example, if the file states.txt has 50 lines:
    $ ruby lines.rb states.txt
    50
    
  10. Flesh out the unit test file for problems 1-6 that I have started here for you. (Note you do not need unit tests for problems 7 and 8.)
    require 'test/unit'
    require 'misc.rb'
    
    class String
      def is_permutation_of(other)
        self.split(//).sort == other.split(//).sort
      end
    end
    
    class TestUtil < Test::Unit::TestCase
    
      def test_strip_vowels()
        assert_equal(strip_vowels(""), "");
        assert_equal(strip_vowels("ouT"), "T");
        # TODO - lots more tests
      end
    
      def test_scramble()
        ["", "a", "aaaa", "aaba", "abfswegwtewr"].each do |s|
          assert(s.is_permutation_of(scramble(s)))
        end
        assert(!"abc".is_permutation_of(scramble("aab")))
      end
    
      def test_powers_of_two()
        # TODO
      end
    
      def test_powers()
        # TODO
      end
    
      def test_interleave()
        assert_equal(interleave([1, 2], [nil, 5, 7, 10]), [1, nil, 2, 5, 7, 10])
        # TODO - lots more tests
      end
    
      def test_stutter()
        assert_equal([1, 1].stutter, [1, 1, 1, 1]);
        # TODO - lots more tests
      end
    
    end
    

Organize your work in your CVS repository. Use the following structure:

homework
  cmsi386
    src
      main
        docs
          hw1.tex
        ruby
          misc.rb (Problems 1-6)
          lines.rb (Problem 7)
          prefixes.rb (Problem 8)
      test
        ruby
          misctest.rb (Problem 9)

Your LaTeX file will contain solutions to each problem, numbered, and in order. You may embed source code in the document, or give the answer as "See <filename>" and submit the sources for these answers at the end of the document. Generate a pdf from the LaTeX file and hand in a printed copy.

Notes