From: Sam Duncan Date: 2011-03-11T08:06:22+09:00 Subject: Re: problem with compilation --------------030206000300000100050603 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit The input string in parse has to match your regex before output exists, however the function will attempt to reverse and return output regardless. On 11/03/11 11:49, Ma Sz wrote: > class Hexagrid > > @@grid = [%w{ 0 1 2 3 }, %w{ 4 5 6 7 }, %w{ 8 9 A B }, %w{ C D E F > }] > > def initialize > @stacks = Hash.new > end > > def parse( string ) > while string =~ /^\s*(\S+)\s*\{([^\}]+)\}\s*;?\s*(.*)/ > > output = '' > stack = @stacks[$1] ||= Hash.new > data = stack[:data] ||= Array.new > pointer = stack[:pointer] ||= [0,0] > > $2.split(//).each do|c| > if c == '>' then pointer[1] += 1 > elsif c == '<' then pointer[1] -= 1 > elsif c == 'v' then pointer[0] += 1 > elsif c == '^' then pointer[0] -= 1 > elsif c == '!' then pointer = [0,0]; data.clear > elsif c == '+' then data.unshift > @@grid[pointer[0]][pointer[1]] > elsif c == 'o' then output<< data.join > end > end > string = $3 > end > return output.reverse > end > > def english( string ) > return parse(string).scan(/.{2}/).collect{|tuple| tuple.hex.chr > }.join > end > end > > hex = Hexagrid.new > while not $stdin.eof? > puts hex.english(gets) > end > --------------030206000300000100050603--