1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import pytest
- def twoSum(nums, target):
- lens = len(nums)
- j=-1
- for i in range(lens):
- if (target - nums[i]) in nums:
-
- if (nums.count(target - nums[i]) == 1) & (target - nums[i] == nums[i]):
- continue
- else:
-
- j = nums.index(target - nums[i], i+1)
- break
- if j > 0:
- return [i, j]
- else:
- return []
-
- def test_case_1():
- res1 = twoSum([2, 3, 1, 5, 8], 6)
- expect1 = [2, 3]
- assert expect1 == res1
- @pytest.mark.parametrize(
- "arr, target, expect",
- [
- ([-2, 3, 1, 5, 8], -1, [0, 2]),
- ([2, 3, 1, 5, 8], 6, [2, 3])
- ]
- )
- def test_cases(arr, target, expect):
- assert twoSum(arr, target) == expect
|