Here are my solutions to the exercises 

On Tuesday 13 June 2006 22:36, Matthew Palmer wrote:
> There's a couple of exercises to be done by about Monday next week, with
> results sent back to the list for comment and improvement:
>
> - Generate a set of 20 random numbers between 1 and 100, and then print
> only the odd ones -- with the special caveat that your solution *must* use
> Array#<< and Array#select.  Don't know what they do?  See the first point
> above.

#!/usr/bin/ruby
a=[]
20.times { x = rand(100) + 1; a << x } 
puts a.select { |v| v if v % 2 == 1 }


> - Read all lines from standard input, then print them out in a random
> order. (Hint: Array#sort_by)  (Extra hint: There's more to the IO class
> than each_line -- there are also methods to read lines)

#!/usr/bin/ruby
a=STDIN.readlines.sort_by {rand}
puts a



-- 
Regards,

Graham Smith
_______________________________________________
coders mailing list
[email protected]
http://lists.slug.org.au/listinfo/coders

Reply via email to