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.
>> strip_vowels("Hello, world")
=> "Hll, wrld"
>> scramble("Hello, world")
=> "w,dlroH elol"
>> powers_of_two(70) {|x| puts x}
1
2
4
8
16
32
64
=> nil
>> powers(3, 400) {|x| puts x}
1
3
9
27
81
243
=> nil
>> interleave(["a", "b"], [1, 2, true, nil]) => ["a", 1, "b", 2, true, nil]
>> [5,4,[3],9].stutter => [5,5,4,4,[3],[3],9,9]
$ ruby prefixes.rb matsumoto m ma mat mats matsu matsum matsumo matsumot matsumoto
$ ruby lines.rb states.txt 50
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