what is the mechanism for `def twoSum(self, nums: List[int], target: int) -> List[int]:` in python 3:
Asked Answered
A

6

16

Ifound the code as follow in python3:

def twoSum(self, nums: List[int], target: int) -> List[int]:
    return sum(nums)

As I know for python def, we only need follow:

def twoSum(self, nums, target):
    return sum(nums)

what is the nums: List[int], target: int and ->List[int] means? Are those new features of python 3? I never see those.

Thanks,

Accompany answered 17/6, 2019 at 16:27 Comment(0)
B
7
from typing import List

def twoSum(nums: List[int], target: int) -> List[int]:
    print(nums, target)

Link: https://docs.python.org/3/library/typing.html

Note The Python runtime does not enforce function and variable type annotations. They can be used by third party tools such as type checkers, IDEs, linters, etc.

Link: https://code.visualstudio.com/docs/python/linting

Bookout answered 30/3, 2020 at 11:12 Comment(1)
after used from typing import List, I still encounter the issue for target: int: TypeError: twoSum() missing 1 required positional argument: 'target'; any ideas?Rosales
C
4

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
            for i in range(len(nums)):
                for j in range(i+1,len(nums)):
                    if target == nums[i]+nums[j]:
                        return [i,j]
                    
                    
num = [1,2,3,4,5,6,7,8,9,10]
target = 8

s = Solution()
print(s.twoSum(num,target))

#output [0,6]

Cupreous answered 19/7, 2021 at 8:37 Comment(0)
P
3

Python has introduced type hinting, which mean we could hinting the type of variable, this was done by doing variable: type (or parameter: type), so for example target is a parameter, of type integer.

the arrow (->) allows us to type hint the return type, which is a list containing integers.

from typing import List
Vector = List[float]

def scale(scalar: float, vector: Vector) -> Vector:
    return [scalar * num for num in vector]

# typechecks; a list of floats qualifies as a Vector.
new_vector = scale(2.0, [1.0, -4.2, 5.4])

In the function greeting, the argument name is expected to be of type str and the return type str. Subtypes are accepted as arguments.

def greeting(name: str) -> str:
    return 'Hello ' + name

Documentation : https://docs.python.org/3/library/typing.html

Passim answered 27/8, 2021 at 4:11 Comment(0)
W
2

" nums : List[int] " states that nums is the name of the list function parameter/variable of type int

" target: int " is another function parameter/variable of type int.

" -> List[int] :" states that the return type of the function must be a list of integers .

Whacky answered 17/11, 2022 at 10:2 Comment(0)
C
1

It's a static typing in python for type checking. It allows you to define the type of input parameters and returns so that certain incompatibilities are dealt with beforehand. They are only annotations, not an actual static typing though. Check mypy package for more.

Connelley answered 17/6, 2019 at 16:55 Comment(0)
P
0

If you've duplicate values in the list, this solution works.

    def twoSum(self, nums: List[int], target: int) -> List[int]:
        for j, k in enumerate(nums):
            for x, y in enumerate(nums):
                if j != x and (k + y) == target:
                    return [j, x]
                    break

   nums = [2,7,11,15]
   target = [9]

   print(Solution().twoSum(nums,target))
Pozzy answered 14/4, 2023 at 20:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.