Python 我如何在自己的IDE中正确地输入自己的测试用例以解决leetcode.com中的问题?

Python 我如何在自己的IDE中正确地输入自己的测试用例以解决leetcode.com中的问题?,python,Python,我是LeetCode.com的新手,遇到了第一个问题() 为上述问题提供了默认代码或模板,但正如我在其他人的解决方案中所观察到的,他们将其简化为: class Solution: def twoSum(self, nums, target): # ... code here ... return [...] 我不使用LeetCode提供的编辑器。我使用我自己的IDE(VSCode)。 每当我尝试创建自己的输入来测试代码时,我都会添加以下行: x = Solution.t

我是LeetCode.com的新手,遇到了第一个问题()

为上述问题提供了默认代码或模板,但正如我在其他人的解决方案中所观察到的,他们将其简化为:

class Solution:
    def twoSum(self, nums, target):
    # ... code here ...
    return [...]
我不使用LeetCode提供的编辑器。我使用我自己的IDE(VSCode)。 每当我尝试创建自己的输入来测试代码时,我都会添加以下行:

x = Solution.twoSum("",[1, 7, 2], 9)
print(x)
被分配给self参数,下面的列表用于nums,而9用于target

最初,我没有

    def twoSum([1, 7, 2], 9)
因此我得到了TypeError:twoSum()缺少1个必需的位置参数:“target”

这就是人们通过将一个随机对象(在我的例子中,它是)分配给self参数来测试代码的方式吗? 还是有其他更干净的方法

我熟悉OOP的基础知识,所以我想可能应该有一个def\uuu init\uu来初始化一些东西。但是,其他人的解决方案并没有利用这一点。

您需要相应地“更改”代码。以以下为例:

假设
leetcode
DSL是

Class Solution:
    def func(self, arg1, arg2):
我将如何对其进行编码(在VScode/ed/ex/vi/vim/gedit/…)如下:

def func(arg1, arg2):    # notice that arg1 and arg2 also have same names
   <my stupid code> (Call this function and pass it input as requested)
def func(arg1,arg2):#注意arg1和arg2也有相同的名称
(调用此函数并按要求传递输入)
然后,在提交时:

我只需在给定函数下复制粘贴


在你的情况下,我会这样做:

我在我最喜欢的编辑器(ed)中的功能

deftowosum(nums:List[int],target:int)->List[int]:
按原样复制粘贴
内容 在解决方案中


对于打印,我只需使用自定义检查器…

您需要相应地“更改”代码。以以下为例:

假设
leetcode
DSL是

Class Solution:
    def func(self, arg1, arg2):
我将如何对其进行编码(在VScode/ed/ex/vi/vim/gedit/…)如下:

def func(arg1, arg2):    # notice that arg1 and arg2 also have same names
   <my stupid code> (Call this function and pass it input as requested)
def func(arg1,arg2):#注意arg1和arg2也有相同的名称
(调用此函数并按要求传递输入)
然后,在提交时:

我只需在给定函数下复制粘贴


在你的情况下,我会这样做:

我在我最喜欢的编辑器(ed)中的功能

deftowosum(nums:List[int],target:int)->List[int]:
按原样复制粘贴
内容 在解决方案中



对于打印,我只需使用自定义检查器…

以下是我用于:

对于twoSum,它将是:

from typing import List
import collections
import itertools
import functools
import math
import string
import random
import bisect
import re
import operator
import heapq
import queue

from queue import PriorityQueue
from itertools import combinations, permutations
from functools import lru_cache
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from collections import Counter


class Solution:
    def twoSum(self, nums, target):
        index_map = {}
        for index, num in enumerate(nums):
            if target - num in index_map:
                return index_map[target - num], index
            index_map[num] = index


print(Solution().twoSum(nums=[2, 7, 11, 15], target=9))
print(Solution().twoSum(nums=[2, 7, 11, 15], target=18))
print(Solution().twoSum(nums=[2, 7, 11, 15], target=22))
输出
工具书类
  • 有关其他详细信息,请参见。这里有很多公认的解决方案、解释、使用各种语言的高效算法以及时间/空间复杂性分析

以下是我使用的模板:

对于twoSum,它将是:

from typing import List
import collections
import itertools
import functools
import math
import string
import random
import bisect
import re
import operator
import heapq
import queue

from queue import PriorityQueue
from itertools import combinations, permutations
from functools import lru_cache
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from collections import Counter


class Solution:
    def twoSum(self, nums, target):
        index_map = {}
        for index, num in enumerate(nums):
            if target - num in index_map:
                return index_map[target - num], index
            index_map[num] = index


print(Solution().twoSum(nums=[2, 7, 11, 15], target=9))
print(Solution().twoSum(nums=[2, 7, 11, 15], target=18))
print(Solution().twoSum(nums=[2, 7, 11, 15], target=22))
输出
工具书类
  • 有关其他详细信息,请参见。这里有很多公认的解决方案、解释、使用各种语言的高效算法以及时间/空间复杂性分析

您需要重构代码。这里有一个技巧:我通常保持函数参数名称不变,然后复制函数中的所有内容并将其粘贴到leetcode函数中;)您需要重构代码。这里有一个技巧:我通常保持函数参数名称不变,然后复制函数中的所有内容并将其粘贴到leetcode函数中;)