I was doing some coding on CodeSignal to refresh my knowledge.
I was asked this question:
Suppose you've got a list of words, let's say ['apple', 'banana', 'apple', 'mango', 'banana']. Each word could be repeated an arbitrary number of times. Think of this list as a conveyor belt in a space-age fruit factory. Now, your task is to identify the last unique fruit on the belt, i.e., the one that didn't repeat. If all the fruits are repeating, then there ain't any unique fruit, and your function should return an empty string ('').
Your function should take a list of strings (the conveyor belt of fruits) as input. Now, a string can be any word, not just a fruit name, and the list can have any number of strings. There could also be an edge case where the list has no strings at all (Empty conveyor belt, eh?). For output, your function should return the last unique string in the list or an empty string if there are not any of them.
To solve this task, you are expected to use sets. Sets are efficient for tracking seen and duplicate elements due to their fast membership testing capability.
and my solution is:
def find_unique_string(words):
last_uniq = ''
seen = set()
for w in words:
if last_uniq == w:
last_uniq = ''
if w not in seen:
last_uniq = w
seen.add(w)
return last_uniq
and the test cases are:
print(find_unique_string(['apple', 'banana', 'apple', 'mango', 'banana'])) # It should print: 'mango'
print(find_unique_string(['hello', 'world', 'hello'])) # It should print: 'world'
print(find_unique_string(['hello', 'world', 'hello', 'world'])) # It should print: ''
print(find_unique_string([])) # It should print: ''
print(find_unique_string(['apple', 'banana', 'apple', 'kiwi', 'banana', 'kiwi'])) # it should print ''
The CodeSignal AI doesn't accept my code as a solution and reports that it is not correct, but it can not provide any cases in which the result of my code is wrong.
Now my question:
Who is right? Me or CodeSignal AI?
If I am right, how can I prove to CodeSignal AI that I am right so I can pass the test and go to the next one?
If AI is right, can you give me a sample test case that breaks my code and doesn't generate the expected output?
Note:
I do not want you to write a solution which would have two sets, one for seen and one for duplicate as this is the result that AI is expecting (I think), but I want to know if my code is correct and if it is, how to prove that it is, or you have a test case to prove that my code is wrong.