简体   繁体   English

在字符串列表中查找唯一字符串

[英]Finding the unique string within an list of strings

I have managed to solve this exercise from the codewars website using the following code我已经设法使用以下代码从codewars网站解决了这个练习

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.空格不重要,只有非空格符号很重要。 Eg string that contains only spaces is like empty string.例如,只包含空格的字符串就像空字符串。

  • It's guaranteed that array / list contains more than 3 strings.保证数组/列表包含超过 3 个字符串。

Any suggestions on how this can be improved to handle such cases or even just general improvement of the function overall.关于如何改进以处理此类情况的任何建议,甚至只是对 function 整体的总体改进。

Thank you.谢谢你。

Calling the list.count method in a loop is inefficient.循环调用list.count方法效率低下。 You can instead use a dict keep track of the unique sets of characters you have iterated through so far.您可以改为使用 dict 来跟踪到目前为止您已迭代的唯一字符集。 Make the set of characters the string minus the space the key of the dict, the string itself the value.将字符集减去空格作为字典的键,将字符串本身作为值。 If the current key is already in the dict, you would then know that the key is not unique.如果当前键已经在字典中,那么您就会知道该键不是唯一的。 The task then becomes to find the key that is different from this common key.然后任务变成找到与该公共密钥不同的密钥。 If there already is a different key in the dict, then return the value of that different key.如果字典中已经有不同的键,则返回该不同键的值。 If not, keep iterating until you get a key that's different from the known common key:如果没有,请继续迭代,直到获得与已知公共密钥不同的密钥:

def find_uniq(arr):
    seen = {}
    common = None
    for string in arr:
        key = frozenset(set(string.lower()).difference(' '))
        if key in seen:
            common = key
        if common is not None:
            if key != common:
                return string
            if len(seen) > 1:
                del seen[common]
                return next(iter(seen.values()))
        seen[key] = string

so that the following expressions will all be True :以便以下表达式都为True

find_uniq(['foo', 'abc', 'acb ']) == 'foo'
find_uniq(['abbc', 'foo', 'acb ']) == 'foo'
find_uniq(['abc', 'acb ', 'foo']) == 'foo'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM