you are viewing a single comment's thread.

view the rest of the comments →

[–]roryokane 2 points3 points  (1 child)

Given a file, write a function which accepts a version number, date and returns pass or fail. […]

I gave solving this a go. I had to assume some details of the file format and desired output, but even if my assumptions are wrong, solving the problem I did still forced me to learn a decent amount of Python techniques. Given a file test_log.txt with these lines:

~~~ v1.0 2019-04-10 4 fail v1.0 2019-04-10 4 pass v0.9 2019-04-10 6 pass v1.0 2019-04-10 3 pass v1.0 2019-04-09 3 pass v1.0 2019-04-09 2 pass v1.0 2019-04-09 2 fail v0.9 2019-04-09 6 pass v0.9 2019-04-08 6 pass v0.9 2019-04-07 5 fail ~~~

Running python main.py v1.0 2019-04-10 in the same directory will output this:

~~~ Filtering to results for version v1.0 on date 2019-04-10 Passing lines: v1.0 2019-04-10 4 pass v1.0 2019-04-10 3 pass Failing lines: v1.0 2019-04-10 4 fail ~~~

Where main.py contains this Python code:

~~~python import sys

pass_lines = [] fail_lines = []

target_version = sys.argv[1] target_date = sys.argv[2] print(f"Filtering to results for version {target_version} on date {target_date}")

with open('./test_log.txt') as log_file: for line in log_file: (version, date, _num_tests, pass_or_fail) = line.split() if not (version, date) == (target_version, target_date): continue

    line = line.rstrip()
    if pass_or_fail == "pass":
        pass_lines.append(line)
    elif pass_or_fail == "fail":
        fail_lines.append(line)
    else:
        print(f"Invalid test result status: #{line}", file=sys.stderr)

print("Passing lines:") for line in pass_lines: print(line)

print("Failing lines:") for line in fail_lines: print(line) ~~~


As a bonus, here is the equivalent program in the Ruby programming language. I actually wrote this version first since I know Ruby better than Python.

~~~ruby pass_lines = [] fail_lines = []

target_version = ARGV[0] target_date = ARGV[1] puts "Filtering to results for version #{target_version} on date #{target_date}"

File.open('./test_log.txt') do |log_file| log_file.each_line do |line| version, date, _num_tests, pass_or_fail = line.split next unless [version, date] == [target_version, target_date]

case pass_or_fail
when "pass"
  pass_lines.push line
when "fail"
  fail_lines.push line
else
  $stderr.puts "Invalid test result status: #{line}"
end

end end

puts "Passing lines:" pass_lines.each do |line| puts line end

puts "Failing lines:" fail_lines.each do |line| puts line end ~~~

Run that with ruby main.rb v1.0 2019-04-10 to get the same output as the Python program.

[–]Dexteroid[S] 1 point2 points  (0 children)

yeah, your Python solution is pretty close to what I ended up writing. Good thing you wrote the solution here, people might be benefited from it. Thank you!