IdeaMonk

thoughts, ideas, code and other things...

Sunday, October 24, 2010

Q1 of BORQ - Mad Libs

My solution for Mad Libs, I need to practice some more regular expression. Rubular is a great help for people like me. RegExp is in a more free-form in Ruby compared to python, which is quite good for quick experiments on irb.
# solution to simpler part
# puts gets.chop.gsub!( /\(\([^))]*\)\)/) { |w| w=gets.chop}
# part 2 - along with substitution
dict = {}
puts IO.read(ARGV.shift).chop.gsub!( /\(\([^))]*\)\)/) { |w|
w = w.scan(/[^()]+/)[0]
combo = w.split(":")
if (combo.size==2)
print "Enter #{combo[1]} : "
w = dict[combo[0]] = gets.chop
else
if (dict[combo[0]] == nil)
print "Enter #{combo[0]} : "
w=gets.chop
else
w = dict[combo[0]]
end
end
}

I wish I could even get rid of the "if"s and make it sexier. Can't think more on this one right now.

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home