1010
1111 < h3 > A grader for a program that outputs the nth Fibonacci number</ h3 >
1212 < pre > < code > import sys, subprocess
13+
1314N = 100
1415cur_fib = 1
1516next_fib = 1
@@ -19,32 +20,37 @@ <h3>A grader for a program that outputs the nth Fibonacci number</h3>
1920 try:
2021 res = subprocess.run(
2122 [sys.executable, sys.argv[1], str(i)],
22- timeout = 5,
23- stdin = subprocess.DEVNULL,
24- stdout = subprocess.PIPE,
25- stderr = subprocess.PIPE,
23+ timeout= 5,
24+ stdin= subprocess.DEVNULL,
25+ stdout= subprocess.PIPE,
26+ stderr= subprocess.PIPE,
2627 )
2728 except subprocess.TimeoutExpired:
28- print("Script timeout for number {}".format(i) )
29+ print(f "Script timeout for number {i}" )
2930 else:
3031 stdout = res.stdout.strip()
3132 if not res.stderr and res.returncode == 0:
3233 try:
3334 if int(stdout) == cur_fib:
3435 score += 1
3536 else:
36- print("Invalid result for number {} (printed {}, answer is {})".format(i, int(stdout), cur_fib))
37+ print(
38+ f"Invalid result for number {i} (printed {int(stdout)}, answer is {cur_fib})"
39+ )
3740 failing_cases.append(i)
3841 except ValueError:
39- print("Non-integer printed for number {}".format(i) )
42+ print(f "Non-integer printed for number {i}" )
4043 failing_cases.append(i)
4144 else:
42- print("Script error for number {}".format(i) )
45+ print(f "Script error for number {i}" )
4346 failing_cases.append(i)
4447 next_fib, cur_fib = cur_fib + next_fib, next_fib
45- print("Score: {}".format( score / N) )
48+ print(f "Score: {score / N}" )
4649
4750with open(sys.argv[4], "a") as logfile:
48- logfile.write("User: {}; Score: {}/{}; Failing test cases: {}\n".format(sys.argv[3], score, N, ", ".join(map(str, failing_cases))))</ code > </ pre >
51+ logfile.write(
52+ f"User: {sys.argv[3]}; Score: {score}/{N}; Failing test cases: {', '.join(str(case) for case in failing_cases)}\n"
53+ )
54+ </ code > </ pre >
4955
5056{% endblock %}
0 commit comments