From: Matthew Moss Date: 2008-08-01T23:45:17+09:00 Subject: [QUIZ] Code Heuristics (#172) -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- The three rules of Ruby Quiz 2: 1. Please do not post any solutions or spoiler discussion for this quiz until 48 hours have passed from the time on this message. 2. Support Ruby Quiz 2 by submitting ideas as often as you can! (A permanent, new website is in the works for Ruby Quiz 2. Until then, please visit the temporary website at . 3. Enjoy! Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone on Ruby Talk follow the discussion. Please reply to the original quiz message, if you can. -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- ## Code Heuristics (#172) This week, your task is to make my job simpler. Each week, coders send in their submissions to Ruby Quiz problems, usually as a mix of quiz discussion and actual code. Your task, then, is to take a submission as input, and generate output that is the extracted code. Every line of the input that isn't code should be prefixed with the comment marker, `#`. Take the following submission to quiz #158: I may have missed it, but I didn't see this solution: require 'enumerator' "Hello world!\n".enum_for(:each_byte).inject($stdout) { |res, b| res << b.chr } Which in 1.9 looks much better: "Hello world!\n".each_char.inject($stdout) { |res, c| res << c } Stefano Your code should take that as input and generate this output: # I may have missed it, but I didn't see this solution: require 'enumerator' "Hello world!\n".enum_for(:each_byte).inject($stdout) { |res, b| res << b.chr } # Which in 1.9 looks much better: "Hello world!\n".each_char.inject($stdout) { |res, c| res << c } # Stefano Assuming you knew for certain which lines should be commented and which should not, your submission would be little more than a glorified `cat`. This quiz is more about identifying Ruby-looking text: what heuristics or patterns will you use to determine what lines or groups of lines are Ruby code or not. Fortunately, there is a large sample set from which you can experiment: all existing Ruby Quiz submissions. Unfortunately -- for this quiz, not in general -- Ruby is very dynamic and flexible, which can make code identification a difficult problem. So I am not expecting perfection here, but I want to see what tricks, or general process, you can come up with for this problem. Extra credit: Provide an optional command-line argument to "split" the output into multiple files. Each separate file would be a different chunk of the code, where code is separated by discussion. Commented discussion should be kept with the code that follows it. -- Matthew Moss