unique(sequence): returns a set with all the elements of sequence, but only one copy, so it should behave as the set function. You cannot use the set function for this implementation.
def unique(sequence):
r = []
for elem in sequence:
if(elem not in r):
r.append(elem)
return rdef test_unique_obvious(self):
# Assign test inputs
test_input = [
[1, 0, 1, 1, 0, 2],
["banana", "apple", "orange"],
[(0, 0), (0, 0), (0, 1)],
]
# Assign test ouputs
test_output = [
{1, 0, 2},
{"banana", "apple", "orange"},
{(0, 0), (0, 1)}
] def unique(sequence):
r = set()
for elem in sequence:
r.add(elem)
return rdef unique(sequence):
r = {sequence[0]}
for elem in sequence[1:]:
r.add(elem)
return runique returns a set with all the unique elements of
sequence. You can assume that sequence is a valid sequence with
elements of any immutable type.
You cannot use the set function to convert the sequence to a set.
def unique(sequence):
return {elem for elem in sequence}