From: Aaron Patterson Date: 2007-11-12T12:20:32+09:00 Subject: Re: comp.lang.fortran challenge On Mon, Nov 12, 2007 at 10:30:06AM +0900, Bil Kleb wrote: > Having Ruby fun with the comp.lang.fortran folks: > > http://tinyurl.com/38o8ex > http://tinyurl.com/2pw22q > > Please riff a better Ruby answer... I'm confused as to why the data needs to be retained in memory..... But here is a little something I put together. It does maintain the datastructures in memory before calculating the results. require 'open-uri' class PhilCollins attr_writer :time alias :time :time= def initialize(io) @uv_by_t = Hash.new { |h,t| h[t] = Hash.new { |n,a| n[a] = [] } } @time = 10 io.each_line do |line| line.gsub!(/:\s*(\d+)[^\d]*$/, '(\1)') line.gsub!(/([UV])\s*\(([^)]*)\)\s=\s(.*)$/, '\1[[\2]] << \3') line.downcase! begin; instance_eval(line); rescue Exception; end end end def u; @uv_by_t[@time]; end alias :v :u def inspect @uv_by_t.sort_by { |t,v| t }.map { |t,v| "for time: #{t}\n" + v.map { |args,vals| "UV(#{args.join(',')}) = #{vals[0] * vals[1]}" if vals.length == 2 }.compact.join("\n") }.join("\n\n") end end p PhilCollins.new(open('http://home.earthlink.net/~dave_gemini/demo.in')) PS, don't try this script at home! It's very dangerous! :-D -- Aaron Patterson http://tenderlovemaking.com/