|
1 | | -import glob |
2 | | -import os |
3 | | -import ast |
4 | | -from sympy import isprime # Import isprime here |
5 | | - |
6 | | - |
7 | | -def run_gcd_test(gcd_func, a, b, expected): |
8 | | - """ |
9 | | - Runs a test case for the gcd function. |
10 | | - """ |
11 | | - result = gcd_func(a, b) |
12 | | - if result == expected: |
13 | | - print(f"PASS: gcd({a}, {b}) = {result}") |
14 | | - else: |
15 | | - print(f"FAIL: gcd({a}, {b}) = {result}, expected {expected}") |
16 | | - |
17 | | - |
18 | | -def load_and_run(): |
19 | | - """ |
20 | | - Detect and run all student submissions from the students_submissions directory. |
21 | | - Each student's submission is expected to be in a file named gcd_<GitHubUsername>.py. |
22 | | - """ |
23 | | - # Find all the files in the students_submissions directory matching the pattern gcd_*.py |
24 | | - student_files = glob.glob("students_submissions/gcd_*.py") |
25 | | - |
26 | | - for file in student_files: |
27 | | - |
28 | | - if file in [ |
29 | | - "students_submissions/gcd_dt393.py", # NameError: name 'math' is not defined. Did you forget to import 'math'? |
30 | | - "students_submissions/gcd_zlk.py", # ZeroDivisionError: integer modulo by zero |
31 | | - ]: |
32 | | - continue |
33 | | - |
34 | | - # Extract the student's GitHub username or identifier from the filename |
35 | | - module_name = os.path.splitext(os.path.basename(file))[0] |
36 | | - |
37 | | - # Read the student's code |
38 | | - with open(file, "r") as f: |
39 | | - student_code = f.read() |
40 | | - |
41 | | - # Parse the student's code to extract all function definitions |
42 | | - parsed_code = ast.parse(student_code) |
43 | | - functions = {} |
44 | | - for node in parsed_code.body: |
45 | | - if isinstance(node, ast.FunctionDef): |
46 | | - func_code = compile( |
47 | | - ast.Module(body=[node], type_ignores=[]), |
48 | | - filename="<ast>", |
49 | | - mode="exec", |
50 | | - ) |
51 | | - func_namespace = {} |
52 | | - exec(func_code, func_namespace) |
53 | | - functions[node.name] = func_namespace[node.name] |
54 | | - |
55 | | - # Check if the gcd function was found and run the test cases |
56 | | - if "gcd" in functions: |
57 | | - gcd_func = functions["gcd"] |
58 | | - # Ensure all necessary functions are available in the namespace |
59 | | - gcd_func.__globals__.update(functions) |
60 | | - gcd_func.__globals__["isprime"] = ( |
61 | | - isprime # Add isprime to the global namespace |
62 | | - ) |
63 | | - print(f"Running {module_name}'s submission...") |
64 | | - # run_gcd_test(gcd_func, 54, 24, 6) |
65 | | - # run_gcd_test(gcd_func, 48, 18, 6) |
66 | | - # run_gcd_test(gcd_func, 101, 10, 1) |
67 | | - # run_gcd_test(gcd_func, 270, 192, 6) |
68 | | - # Edge case test cases |
69 | | - run_gcd_test(gcd_func, 0, 0, None) # Both numbers are zero |
70 | | - run_gcd_test(gcd_func, -54, 24, 6) # One negative number |
71 | | - run_gcd_test(gcd_func, 54, -24, 6) # One negative number |
72 | | - run_gcd_test(gcd_func, -54, -24, 6) # Both numbers are negative |
73 | | - run_gcd_test(gcd_func, 0, 24, 24) # One number is zero |
74 | | - run_gcd_test(gcd_func, 24, 0, 24) # One number is zero |
75 | | - run_gcd_test(gcd_func, 17, 13, 1) # Both numbers are prime |
76 | | - run_gcd_test(gcd_func, 1000000000, 2, 2) # Large number and small number |
77 | | - run_gcd_test(gcd_func, 123456789, 987654321, 9) # Large numbers |
78 | | - run_gcd_test(gcd_func, 1, 1, 1) # Both numbers are one |
79 | | - print() |
80 | | - else: |
81 | | - print(f"ERROR: {module_name}'s submission does not have a gcd function\n") |
82 | | - |
83 | | - |
84 | | -# This is the entry point of the script |
85 | | -if __name__ == "__main__": |
86 | | - load_and_run() |
| 1 | +import glob |
| 2 | +import os |
| 3 | +import ast |
| 4 | +from sympy import isprime # Import isprime here |
| 5 | + |
| 6 | + |
| 7 | +def run_gcd_test(gcd_func, a, b, expected): |
| 8 | + """ |
| 9 | + Runs a test case for the gcd function. |
| 10 | + """ |
| 11 | + result = gcd_func(a, b) |
| 12 | + if result == expected: |
| 13 | + print(f"PASS: gcd({a}, {b}) = {result}") |
| 14 | + else: |
| 15 | + print(f"FAIL: gcd({a}, {b}) = {result}, expected {expected}") |
| 16 | + |
| 17 | + |
| 18 | +def load_and_run(): |
| 19 | + """ |
| 20 | + Detect and run all student submissions from the students_submissions directory. |
| 21 | + Each student's submission is expected to be in a file named gcd_<GitHubUsername>.py. |
| 22 | + """ |
| 23 | + # Find all the files in the students_submissions directory matching the pattern gcd_*.py |
| 24 | + student_files = glob.glob("students_submissions/gcd_*.py") |
| 25 | + |
| 26 | + for file in student_files: |
| 27 | + |
| 28 | + if file in [ |
| 29 | + "students_submissions/gcd_dt393.py", # NameError: name 'math' is not defined. Did you forget to import 'math'? |
| 30 | + "students_submissions/gcd_zlk.py", # ZeroDivisionError: integer modulo by zero |
| 31 | + ]: |
| 32 | + continue |
| 33 | + |
| 34 | + # Extract the student's GitHub username or identifier from the filename |
| 35 | + module_name = os.path.splitext(os.path.basename(file))[0] |
| 36 | + |
| 37 | + # Read the student's code |
| 38 | + with open(file, "r") as f: |
| 39 | + student_code = f.read() |
| 40 | + |
| 41 | + # Parse the student's code to extract all function definitions |
| 42 | + parsed_code = ast.parse(student_code) |
| 43 | + functions = {} |
| 44 | + for node in parsed_code.body: |
| 45 | + if isinstance(node, ast.FunctionDef): |
| 46 | + func_code = compile( |
| 47 | + ast.Module(body=[node], type_ignores=[]), |
| 48 | + filename="<ast>", |
| 49 | + mode="exec", |
| 50 | + ) |
| 51 | + func_namespace = {} |
| 52 | + exec(func_code, func_namespace) |
| 53 | + functions[node.name] = func_namespace[node.name] |
| 54 | + |
| 55 | + # Check if the gcd function was found and run the test cases |
| 56 | + if "gcd" in functions: |
| 57 | + gcd_func = functions["gcd"] |
| 58 | + # Ensure all necessary functions are available in the namespace |
| 59 | + gcd_func.__globals__.update(functions) |
| 60 | + gcd_func.__globals__["isprime"] = ( |
| 61 | + isprime # Add isprime to the global namespace |
| 62 | + ) |
| 63 | + print(f"Running {module_name}'s submission...") |
| 64 | + # run_gcd_test(gcd_func, 54, 24, 6) |
| 65 | + # run_gcd_test(gcd_func, 48, 18, 6) |
| 66 | + # run_gcd_test(gcd_func, 101, 10, 1) |
| 67 | + # run_gcd_test(gcd_func, 270, 192, 6) |
| 68 | + # Edge case test cases |
| 69 | + run_gcd_test(gcd_func, 0, 0, None) # Both numbers are zero |
| 70 | + run_gcd_test(gcd_func, -54, 24, 6) # One negative number |
| 71 | + run_gcd_test(gcd_func, 54, -24, 6) # One negative number |
| 72 | + run_gcd_test(gcd_func, -54, -24, 6) # Both numbers are negative |
| 73 | + run_gcd_test(gcd_func, 0, 24, 24) # One number is zero |
| 74 | + run_gcd_test(gcd_func, 24, 0, 24) # One number is zero |
| 75 | + run_gcd_test(gcd_func, 17, 13, 1) # Both numbers are prime |
| 76 | + run_gcd_test(gcd_func, 1000000000, 2, 2) # Large number and small number |
| 77 | + run_gcd_test(gcd_func, 123456789, 987654321, 9) # Large numbers |
| 78 | + run_gcd_test(gcd_func, 1, 1, 1) # Both numbers are one |
| 79 | + print() |
| 80 | + else: |
| 81 | + print(f"ERROR: {module_name}'s submission does not have a gcd function\n") |
| 82 | + |
| 83 | + |
| 84 | +# This is the entry point of the script |
| 85 | +if __name__ == "__main__": |
| 86 | + load_and_run() |
0 commit comments