On Tue, 2006-06-13 at 22:36 +1000, Matthew Palmer wrote:
As requested:
> - 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
x=[]
20.times {x<<rand(100)+1}
x.select {|item| puts item if item%2==0}
> - 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
lines=[]
while input = gets do lines<<input.chomp end
# Change method of input just to see what happens:
puts "Please enter more stuff .. trying $stdin instead of gets"
$stdin.each do |input| lines<<input.chomp end
puts "Output starts here:"
puts lines.sort_by {rand}
> Also, please e-mail the code you wrote tonight to solve David's rounding
> problem to the list
#!/usr/bin/ruby
x=ARGV[0].to_f
y=x%5
if y > 2
z=x-y+5
else
z=x-y end
puts z
_______________________________________________
coders mailing list
[email protected]
http://lists.slug.org.au/listinfo/coders