For the first homework assignment, you'll be warming up to
Ruby. The due date is Wednesday, September 17 for graduates
and Thursday, September 18 for undergraduates.
- 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
- 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
- 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"
- 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"
- 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
- 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
- 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:
>> interleave(["a", "b"], [1, 2, true, nil])
=> ["a", 1, "b", 2, true, nil]
- 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]