I have managed to solve this exercise from the codewars website using the following code
def find_uniq(arr):
diff = None
first = None
for s in arr:
if first is None:
first = s
continue
if (len(first) in [0, 1]) and (len(s) in [0, 1]):
if arr.count(first) == 1:
return first
elif arr.count(s) == 1:
return s
temp = set(first.lower()).difference(set(s.lower()))
if temp:
diff = s
if diff is not None:
temp = set(diff.lower()).difference(set(s.lower()))
return first if temp else diff
My code passes all their unit tests but problem is when I try it using the following custom unit test, it fails
test.assert_equals(['foo', 'abc', 'acb', 'bac', 'bca', 'cab', 'cba dab'], 'foo') # fails, returns 'abc'
The exercise is a follows:
There is an array / list of strings. All strings contains similar letters except one. Try to find it!
and rules are:
Strings may contain spaces. Spaces is not significant, only non-spaces symbols matters. E.g. string that contains only spaces is like empty string.
It’s guaranteed that array / list contains more than 3 strings.
Any suggestions on how this can be improved to handle such cases or even just general improvement of the function overall.
Thank you.