【问题标题】:Binary Search Iterative and Recursive with tuple (python)使用元组进行二进制搜索迭代和递归(python)
【发布时间】:2018-03-11 16:51:03
【问题描述】:

我正在尝试创建参数为 (tuple, int) 的二进制搜索(迭代和递归)。我真的很想理解它,因为我认为我在逻辑上理解了代码,但显然不是。

迭代代码 (isMemberI) 有一半时间给了我正确的结果,然后随机不正确,所以我不知道是什么原因造成的。我没有收到任何错误,有时只是错误的答案。

递归代码有时会给我带来错误,然后可能会在我的测试用例的 1/4 中起作用。当我尝试以下操作时,出现以下错误:

>>> isMemberR((1, 2, 3, 3, 4), 4
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    isMemberR((1, 2, 3, 3, 4), 4)
  File "/Users/alyssakelley/Documents/p93_binsearch.py", line 127, in     isMemberR
    return isMemberR(aseq[midpoint + 1], target)
  File "/Users/alyssakelley/Documents/p93_binsearch.py", line 117, in isMemberR
    if len(aseq) == 0:
TypeError: object of type 'int' has no len()

>>> isMemberR('aeiou', 'y')
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    isMemberR('aeiou', 'y')
  File "/Users/alyssakelley/Documents/p93_binsearch.py", line 127, in isMemberR
    return isMemberR(aseq[midpoint + 1], target)
  File "/Users/alyssakelley/Documents/p93_binsearch.py", line 127, in isMemberR
    return isMemberR(aseq[midpoint + 1], target)
IndexError: string index out of range


>>> isMemberR((1, 3, 5, 7), 4)
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    isMemberR((1, 3, 5, 7), 4)
  File "/Users/alyssakelley/Documents/p93_binsearch.py", line 125, in isMemberR
    return isMemberR(aseq[:midpoint], target)
  File "/Users/alyssakelley/Documents/p93_binsearch.py", line 127, in isMemberR
   return isMemberR(aseq[midpoint + 1], target)
IndexError: tuple index out of range

这是我的代码:

def isMemberI(aseq, target):
    first = 0
    last = len(aseq) - 1
    found = False

    while first < last:
        midpoint = (first + last) // 2
        if aseq[midpoint] == target:
            found = True

        else:
            if target < aseq[midpoint]:
                last = midpoint - 1
            else:
                first = midpoint + 1
    return found 

def isMemberR(aseq, target):
    if len(aseq) == 0:
        return False
    else:
        midpoint = len(aseq) // 2
        if aseq[midpoint] == target:
            return True
        else:
            if target < aseq[midpoint]:
                return isMemberR(aseq[:midpoint], target)
            else:
                return isMemberR(aseq[midpoint + 1], target)

我正在尝试的测试用例和我期望的结果:

>>> isMemberI((1, 2, 3, 3, 4), 4)
True

>>> isMemberI((1, 2, 3, 3, 4), 2)
True

>>> isMemberI('aeiou', 'i')
True

>>> isMemberI('aeiou', 'y')
False

>>> isMemberI((1, 3, 5, 7), 4)
False

>>> isMemberI((23, 24, 25, 26, 27), 5)
False

>>> isMemberI((0, 1, 4, 5, 6, 8), 4)
True

>>> isMemberI((0, 1, 2, 3, 4, 5, 6), 3)
True

>>> isMemberI((1, 3), 1)
True

>>> isMemberI((2, 10), 10)
True

>>> isMemberI((99, 100), 101)
False

>>> isMemberI((42,), 42)
True

>>> isMemberI((43,), 44)
False

>>> isMemberI((), 99)
False
'''

【问题讨论】:

  • aseq[midpoint + 1] 在 isMemberR(aseq[midpoint + 1], target) asequence 中吗?不是。

标签: python recursion binary-search


【解决方案1】:

这里最后一个函数调用是在索引 midpoint+1 处传递一个值,而不是 tuple 。

错误解释

IndexError:元组索引超出范围,当元组的len为1且midpoint+1为1超出范围时会发生这种情况,因为只有一个元素

TypeError: 'int' 类型的对象没有 len() ,此时 int 在最后一行作为第一个参数传递给 isMemberR

def isMemberR(aseq, target):
    if len(aseq) == 0:
        return False
    else:
        midpoint = len(aseq) // 2
        if aseq[midpoint] == target:
            return True
        else:
            if target < aseq[midpoint]:
                return isMemberR(aseq[:midpoint], target)
            else:
                return isMemberR(aseq[midpoint+1:], target)

【讨论】:

  • 太棒了!感谢您解释:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-20
  • 2015-01-21
  • 1970-01-01
相关资源
最近更新 更多