【问题标题】:I am getting a Error in this program with output我在这个程序中得到一个错误的输出
【发布时间】:2020-11-04 07:46:16
【问题描述】:

这是一个来自 leetcode 的程序。当我当时在我的计算机上执行此代码时,它没有显示任何错误。但是当我在 leetcode 中运行相同的代码时,我开始得到这个错误以及输出。

class Solution:
    def __init__(self ,nums,target):
        self.nums = []
        self.target = target
        self.solution = []

    def check():
        for i in nums :
            if -10**9 <= nums[i] <= 10**9 :
                print("ok")
            else :
                sys.exit()
        if -10**9 <= target <= 10**9 :
            print("ok")
        else :
            sys.exit()

    try :
        def twosum(self):
            if  2 <= len(nums) <= 10**5 :
                for x in nums :
                    for y in nums :
                        if y != x and x+y == target :
                            solution = []
                            solution.append(nums.index(x))
                            solution.append(nums.index(y))
                            print("Because nums[" + str(nums.index(x)) + "] + nums[" + str(nums.index(y)) + "] == " + str(target) + ", we return" , solution)
                            return solution
                            break
                        else :
                            continue
    except :
        print("An exception occured")



nums = [1,2,3,4,5,6]
target = 4
demo = Solution(nums,target)
print(demo.twosum())

我得到了所需的输出:

    Because nums[0] + nums[1] == 9, we return [0, 1]
    [0, 1]

但我也遇到了错误:

TypeError: init() 缺少 2 个必需的位置参数:“nums”和“target” ret = Solution().twoSum(param_1, param_2) _driver (Solution.py) 中的第 62 行 _司机() (Solution.py) 中的第 73 行

【问题讨论】:

    标签: python-3.x oop


    【解决方案1】:

    从错误输出中,leetcode 驱动程序似乎想要调用您的代码:

    ret = Solution().twoSum(param_1, param_2)
    

    换句话说,它想用 no 参数构造你的类,然后调用你类中的特定函数来完成工作,将参数传递给 that 函数.

    这意味着您必须提供以下内容:

    class Solution:
        def __init__(self):
            pass
    
        def twosum(self, nums, target):
            # Do your grunt work here.
    

    而且,如果你想使用该异常,它应该在 inside twosum 函数中。您现在拥有的方式是try/excepttwosum 函数的创建 期间处于活动状态,而不是在运行 函数时处于活动状态。


    此外,您应该摆脱或调整自己的驱动程序代码 - 这就是您看到预期输出的原因。但是leetcode 并不想要你这样做,它只是希望你提供类以便它的驱动程序可以调用它。

    很有可能它会简单地导入您的文件然后调用您的函数,因此如果您正常运行它,您可以使用__name__ 技巧来使用您的驱动程序代码,或者如果他们导入它,则使用他们的驱动程序代码。

    综上所述,您最终可能会得到如下结果:

    class Solution:
        def __init__(self):
            pass
    
        def twosum(self, nums, target):
            if  2 <= len(nums) <= 10**5:
                for x in nums:
                    for y in nums:
                        if y != x and x+y == target:
                            solution = []
                            solution.append(nums.index(x))
                            solution.append(nums.index(y))
                            return solution
    
    if __name__ == "__main__": # don't run if you get imported.
        nums = [1,2,3,4,5,6]
        target = 4
        demo = Solution(nums, target)
        print(demo.twosum())
    

    请记住,我不知道您的 twosum 函数是否正在做它应该做的事情,我只是在 return 之后整理了像 break 这样不必要的东西。但这应该是一个很好的基础,可以根据 leetcode 对您的需求来测试您的代码。

    【讨论】:

      【解决方案2】:

      我们通常会使用字典来解决它:

      class Solution:
          def twoSum(self, nums, target):
              indices = {}
              for index, num in enumerate(nums):
                  if target - num in indices:
                      return indices[target - num], index
                  indices[num] = index
      
      • 内存 O(N)

      • 运行时 O(N)


      您也可以查看解决方案here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-25
        • 2022-01-19
        • 2014-01-24
        • 1970-01-01
        • 2021-03-29
        • 2022-01-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多