Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Ruby
[QUIZ] Weird Numbers (#57) Solution
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Ryan Leavengood, post: 4495142"] Well I coded this up. Instead of pasting my whole solution I'll just paste the cache class and the updated "main" block for my solution. I've also added some timing code so one can see the speed improvements. To try this out, replace my if $0 =3D=3D __FILE__ code from my original solution with all of the following: class WeirdCache def initialize(filename=3D'.weirdcache') @filename =3D filename if test(?e, filename) @numbers =3D IO.readlines(filename).map do |i| i.chomp.to_i end else @numbers=3D[] end @added =3D false end def each(&block) @numbers.each(&block) end def <<(i) @added =3D true @numbers << i end def save if @added File.open(@filename, File::RDWR|File::CREAT|File::TRUNC) do |file| file.puts @numbers end end end end if $0 =3D=3D __FILE__ if ARGV.length < 1 puts "Usage: #$0 <upper limit>" exit(1) end puts "Weird numbers up to and including #{ARGV[0]}:" start =3D Time.now cache =3D WeirdCache.new at_exit {cache.save} limit =3D ARGV[0].to_i i =3D 69 cache.each do |i| if i <=3D limit puts i end end (i+1).upto(limit) do |j| if j.weird? cache << j puts j end end puts "This took #{Time.now - start} seconds" end [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Ruby
[QUIZ] Weird Numbers (#57) Solution
Top