#C12932. Unique List Implementation
Unique List Implementation
Unique List Implementation
You are required to implement a class UniqueList
that works similarly to a built-in list, but it only stores unique elements. That is, duplicate values are not allowed.
The UniqueList
class should support the following operations:
- Initialization: When initialized with a list of integers, only the first occurrence of each element is kept (preserving the order).
- add x: Adds the integer x to the list if it is not already present.
- remove x: Removes the integer x from the list. If x is not present, output an error message exactly as: \(\text{ValueError: Item 'x' not found}\).
- get i: Retrieves the element at index i (0-indexed). It is guaranteed that i is a valid index.
- print: Outputs the entire list in a format identical to Python's list string representation. For example:
[1, 2, 3]
.
You need to read input from standard input (stdin) and write output to standard output (stdout). The input format is described below.
Note: If a remove
operation is attempted on a non-existent element, output the error message and continue processing subsequent operations.
Implementation Details:
- You must use the provided operations and follow the input/output requirements exactly.
- The error message when removal fails must be in LaTeX format when describing the rule, i.e., using \(\text{ValueError: Item 'x' not found}\) in the description. The actual output should match the string exactly.
inputFormat
The first line contains an integer n
, the number of elements in the initial list.
The second line contains n
space-separated integers representing the initial list. Duplicates may be present; only the first occurrence should be kept.
The third line contains an integer m
, the number of operations to perform.
The following m
lines each contain one of the following commands:
add x
— add integer x to the list (if not already present).remove x
— remove integer x from the list. If x is not present, output the error message.get i
— output the element at index i (0-indexed).print
— output the current list.
outputFormat
For each get
and print
command, output the result on a separate line in the order they appear.
If a remove
command is attempted with a non-existent element, output the following error message exactly:
ValueError: Item 'x' not found
There is no output for add
commands or successful remove
commands.
3
1 2 3
4
add 4
add 2
get 1
print
2
[1, 2, 3, 4]
</p>