Skip to content

Instantly share code, notes, and snippets.

@ntalbott
Last active May 14, 2019 21:06
Show Gist options
  • Save ntalbott/829eb0a0c26754e73c36404319e58d2b to your computer and use it in GitHub Desktop.
Save ntalbott/829eb0a0c26754e73c36404319e58d2b to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
CASES = {
"" => ["", "", "", "", ""],
"Some random data." => ["Some random data.", "", "", "", ""],
"This information should be ignored RECORD_NUM 02 This is the information for record 2 RECORD_NUM 01 This is the information for record 1 RECORD_NUM 04 This is the information for record 4" =>
[" This is the information for record 1 ", " This is the information for record 2 ", "", " This is the information for record 4", ""],
}
def process(input, expected_record_count=5)
return ([input] + ([""] * (expected_record_count-1))) unless input =~ /RECORD_NUM/
(1..expected_record_count).collect do |num|
input =~ /RECORD_NUM #{sprintf("%02i", num)}(.+?)(?=RECORD_NUM|$)/ ? $1 : ""
end
end
def process1(input, expected_record_count=5)
return ([input] + ([""] * (expected_record_count-1))) unless input =~ /RECORD_NUM/
records = Hash[input.scan(/RECORD_NUM 0?(\d+)(.+?)(?=RECORD_NUM|$)/)]
(1..expected_record_count).collect{|num| records[num.to_s] || ""}
end
CASES.each do |input, expected_output|
actual_output = process1(input)
if actual_output != expected_output
puts "-> EXPECTED"
puts expected_output.inspect
puts "-> BUT WAS"
puts actual_output.inspect
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment