In what concerns the continuous evaluation solving exercises grade during the semester, you should submit until 23:59 of January 3rd
(this exercise will still be available for submission after that deadline, but without counting towards your grade)
[to understand the context of this problem, you should read the class #11 exercise sheet]
In this problem you should read from the standard input using functions such as input() and you should write to the standard output using functions such as print()
"Cypher, you've made the right decision. Your desire to return to the Matrix... to a world free from the suffering of reality... is understandable. All we need is for you to sabotage their mission. The rest will fall into place."
"You’re right, Smith.", Cypher says. "This glitch will be perfect."
Cypher pulls out a device from his pocket: a small chip that he's planted inside the Nebuchadnezzar.
"This little friend is doing to create errors in their code", Cypher grins.
"I see.", Smith nods. "So, you’re hoping to confuse them, halt their progress. A simple trick, really. But it will buy us time. I will make sure the agents are in position to capture them while they are occupied with this... glitch."
Cypher nods, a smug look crossing his face. He’s convinced that this simple flaw will be enough to bring down the crew. But little does he know, Neo is catching these glitches...
Cypher has done his part. The glitch is in place. Now, it's up to you to help Neo find the errors in the code!
Write a program that reads a bunch of Python expressions and finds what expressions contain errors, how many times each error type appears, and prints a list of the types of errors that appear, sorted in decreasing order of frequency (and in case of a tie in increasing order of name).
You should use the eval() function to evaluate Python expressions (and you already know how to extract an error type and its name, right?)
The first line of input contains an integer N, the number of Python expressions to evaluate. The following N lines each contain an expression.
The output should contain as many lines as error types, in the format FREQUENCY ERROR_TYPE. The errors should come in decreasing order of frequency and in case of a tie in increasing alphabeteical order of the error type name.
The following limits are guaranteed in all the test cases that will be given to your program:
| 1 ≤ N ≤ 100 | Number of lines to process |
| Example Input | Example Output |
16
1 / 0
1 % 0
1 + 2
[1, 2, 3]
2 + 3 + 4 +
[1, 2
3, 4]
AgentSmith
int("[]")
int("is this real life?")
{"a":2}["a"]
{"a":2}["b"]
[4,5,6][0]
[4,5,6][8]
open("smith.txt")
[0]*(2**100)
|
3 SyntaxError 2 ValueError 2 ZeroDivisionError 1 FileNotFoundError 1 IndexError 1 KeyError 1 NameError 1 OverflowError |
Explanation of example input:
"1 / 0" creates a ZeroDivisionError error: "division by zero"
"1 % 0" creates a ZeroDivisionError error: "integer division or modulo by zero"
"1 + 2" is a valid expression
"[1, 2, 3]" is a valid expression
"2 + 3 + 4 +" creates a SyntaxError error: "unexpected EOF while parsing ("[1, 2" creates a SyntaxError error: "unexpected EOF while parsing ("3, 4]" creates a SyntaxError error: "unmatched ']' ("AgentSmith" creates a NameError error: "name 'AgentSmith' is not defined"
"int("[]")" creates a ValueError error: "invalid literal for int() with base 10: '[]'"
"int("is this real life?")" creates a ValueError error: "invalid literal for int() with base 10: 'is this real life?'"
"{"a":2}["a"]" is a valid expression
"{"a":2}["b"]" creates a KeyError error: "'b'"
"[4,5,6][0]" is a valid expression
"[4,5,6][8]" creates a IndexError error: "list index out of range"
"open("smith.txt")" creates a FileNotFoundError error: "[Errno 2] No such file or directory: 'smith.txt'"
"[0]*(2**100)" creates a OverflowError error: "cannot fit 'int' into an index-sized integer"