in reply to search multiple lists
Smells like homework.
require 'pp' class Array def shuffle!( ) each_index {|i| j = rand(i+1); self[i], self[j] = self[j], self[i] +} end def shuffle( ) self.dup.shuffle! end end lists = (1..6).inject( [] ) { |m,o| m << [] } ( 1..50 ).to_a.shuffle.each { |x| lists[ rand(6) ] << x } target = Integer( rand( 50 ) + 1 ) pp lists list_no = nil lists.each_index { |i| list_no = i if lists[i].any? { |x| x == target +} } puts "#{target} is in list #{list_no}"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: search multiple lists
by Annemarie (Acolyte) on Mar 04, 2005 at 08:18 UTC | |
by chb (Deacon) on Mar 04, 2005 at 08:39 UTC |