#K92132. List Modification with Remove and Discard Commands
List Modification with Remove and Discard Commands
List Modification with Remove and Discard Commands
You are given a list of strings and a sequence of commands to modify the list. Each command is either a remove x
or discard x
operation.
The remove command attempts to remove the first occurrence of x from the list. If x is not present, the program should immediately output an error message of the form \(\texttt{list.remove(x): x not in list}\) and terminate without processing any further commands.
The discard command removes the first occurrence of x from the list if it exists; if it does not, do nothing and continue processing subsequent commands.
After processing all commands (unless terminated early by an error), output the modified list as a space-separated string.
Note: All formulas must be presented in \(\LaTeX\) format when required.
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains an integer \(n\) representing the number of elements in the list.
- The second line contains \(n\) space-separated strings, the elements of the list.
- The third line contains an integer \(N\) representing the number of commands.
- The next \(N\) lines each contain a command in the format "remove x" or "discard x".
outputFormat
Output to standard output (stdout) either:
- If a
remove
command fails because the element is not in the list, print an error message of the format \(\texttt{list.remove(x): x not in list}\) (with the appropriate element in place of \(x\)) and exit. - If all commands are processed successfully, print the final list as a space-separated string.
4
apple banana cherry date
5
remove banana
remove date
discard apple
remove fig
discard cherry
list.remove(fig): fig not in list