为什么我的函数返回两个重复的值?

发布于 2025-01-20 14:18:40 字数 800 浏览 1 评论 0原文

我正在做一个练习问题,我需要在字符串列表中找到最长的公共前缀。

示例:

Input: strs = ["flower","flow","flight"]
Output: "fl"

我的处理方法如下:

def my_function(strs):

  ### get the length of shortest string in list and assign to "window"

  min_len = min([len(x) for x in strs])
  window = min_len

  ### chop all items in list to length of shortest item

  for i in range(window):
    if window == 0:

      return ''

    strs = [x[:window] for x in strs]

  ### Check the length of the set() of all items. If it's longer than 1, 
  ### shorten the window and re-check

    if len(set(strs)) == 1:
      answer = list(set(strs))
      
      return answer[0]

    else: 
      window += -1

当我使用上面给出的测试用例时,我的函数返回:

fl
fl

为什么我会得到这个重复?

I'm doing a practice problem where I need to find the longest common prefix in a list of strings.

Example:

Input: strs = ["flower","flow","flight"]
Output: "fl"

I'm approaching this as follows:

def my_function(strs):

  ### get the length of shortest string in list and assign to "window"

  min_len = min([len(x) for x in strs])
  window = min_len

  ### chop all items in list to length of shortest item

  for i in range(window):
    if window == 0:

      return ''

    strs = [x[:window] for x in strs]

  ### Check the length of the set() of all items. If it's longer than 1, 
  ### shorten the window and re-check

    if len(set(strs)) == 1:
      answer = list(set(strs))
      
      return answer[0]

    else: 
      window += -1

When I use the test case given above, my function returns:

fl
fl

Why am I getting this duplication?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑梦风尘 2025-01-27 14:18:40

您正在使用一个怪异的循环,其中将“ for”递增“窗口”,但根据条件将其降低。这会导致不一致的迭代。

循环使用,然后手动修改计数器,或者对循环使用,并且不要修改计数器。

这说明您的方法不是那么有效,而是在位置后平行地检查字符:

def prefix(strs):
    out = []
    for x in zip(*strs):
        if len(set(x)) == 1:
            out.append(x[0])
        else:
            break
    return ''.join(out)

prefix(strs)

'fl'

You're using a weird loop where you increment "window" with the "for", but decrement it based on a condition. This causes an inconsistent iteration.

Either use a while loop and modify the counter manually, or use a for loop and don't modify the counter.

That said your approach of not so efficient, rather check the characters in parallel position after position:

def prefix(strs):
    out = []
    for x in zip(*strs):
        if len(set(x)) == 1:
            out.append(x[0])
        else:
            break
    return ''.join(out)

prefix(strs)

Output: 'fl'

蓝咒 2025-01-27 14:18:40

在 @Mozway的帮助下,我能够使用成功执行该函数,而语句:

def prefix(strs):
  min_len = min([len(x) for x in strs])
  window = min_len

  for i in range(window):
    while window > 0:
      strs = [x[:window] for x in strs]
      
      if len(set(strs)) == 1:
        answer = list(set(strs))

        return answer[0]

      else:
        window += -1
      
  return ''

With @mozway's help, I was able to successfully execute the function using a while statement:

def prefix(strs):
  min_len = min([len(x) for x in strs])
  window = min_len

  for i in range(window):
    while window > 0:
      strs = [x[:window] for x in strs]
      
      if len(set(strs)) == 1:
        answer = list(set(strs))

        return answer[0]

      else:
        window += -1
      
  return ''
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文