Gautam D. [email protected] writes:
Here is my solution,
and this is mine, actually quite different from the ones I saw; it
uses gsubs to reduce step-by-step.
def unwrap(desc)
[desc.gsub!(‘BB’, ‘B’), desc.gsub!(‘(B)’, ‘B’),
desc.gsub!(‘[B]’, ‘B’), desc.gsub!(‘{B}’, ‘B’)].nitems > 0
end
def valid?(desc)
desc = desc.dup
true while unwrap desc
desc == “B”
end
packet = ARGV.first.to_s
if valid? packet
puts packet
exit 0
else
exit 1
end
Here’s my solution for checking outputs.
James Edward G. II
#!/usr/local/bin/ruby -w
require “strscan”
stack = Array.new
input = StringScanner.new(ARGF.read)
until input.eos?
if input.scan(/[[({]/)
stack.push(input.matched)
elsif input.scan(/[])}]/)
exit(1) unless “#{stack.pop}#{input.matched}” =~ /\A(?:[]|()|
{})\Z/
else
input.scan(/[^[](){}]+/)
end
end
Here’s my ruby n00b solution, I intended to fix erroneous strings, but I
may
not have the time, so i’ll leave it at reporting the error.