"""
Source: https://leetcode.com/problems/two-sum/#/description

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:
Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

"""
import unittest

def leetcode_twosum(nums, target):
    for i in range(target):
        try:
            idx_0 = nums.index(i)
            nums[idx_0] = 'x'
            idx_1 = nums.index(target - i)
            return [idx_0, idx_1] if idx_0 < idx_1 else [idx_1, idx_0]
        except:
            continue


class TestStringMethods(unittest.TestCase):

    def test_sorted_twosum(self):
        self.assertEqual(
            leetcode_twosum([2, 7, 11, 15], 9), 
            [0, 1]
        )
        self.assertEqual(
            leetcode_twosum([2, 7, 11, 15], 18), 
            [1, 2]
        )

    def test_unsorted_twosum(self):
        self.assertEqual(
            leetcode_twosum([3, 2, 9, 11, 7, 4], 5), 
            [0, 1]
        )
        self.assertEqual(
            leetcode_twosum([3, 2, 9, 11, 7, 4], 20), 
            [2, 3]
        )
        self.assertEqual(
            leetcode_twosum([3, 22, 9, 11, 7, 4], 15), 
            [3, 5]
        )

    def test_duplicated_twosum(self):
        self.assertEqual(
            leetcode_twosum([3, 3, 5, 7, 9], 6), 
            [0, 1]
        )

    def test_zero_twosum(self):
        self.assertEqual(
            leetcode_twosum([0, 4, 3, 0], 0), 
            [0, 3]
        )

if __name__ == '__main__':
    unittest.main()
@@ -449,24 +449,37 @@
 or i
- in range(target
+dx_0, val_0 in enumerate(nums
 ):%0A 
@@ -510,80 +510,30 @@
 idx_
-0
+1
  = nums
-.index(i)%0A            nums%5Bidx_0%5D = 'x'%0A            idx_1 = nums
+%5B(idx_0 + 1):%5D
 .ind
@@ -544,17 +544,21 @@
 arget - 
-i
+val_0
 )%0A      
@@ -587,45 +587,20 @@
 dx_1
-%5D if
+ +
  idx_0 
-%3C idx_1 else %5Bidx_1, idx_0
++ 1
 %5D%0A  
@@ -634,16 +634,25 @@
 ontinue%0A
+        %0A
 %0A%0Aclass 
@@ -1580,16 +1580,157 @@
      )%0A%0A
+    def test_negative_twosum(self):%0A        self.assertEqual(%0A            leetcode_twosum(%5B0, 4, 3, -2%5D, 2), %0A            %5B1, 3%5D%0A        )%0A%0A%0A
 if __nam

Add negative use case for leetcode twosum