Brush the title of a given integer array nums and a target taget, and you find the target value of the two integers in the array

 This afternoon, a moment github, it would like to brush a problem, it turned out the brush turned a deaf ear to improve their ability to solve practical problems in the course of the interview, we found that, in fact, often, the interviewer gives us question, there is also a certain degree of randomness, so we have to brush more questions. To find the problem.

   

  topic:  

    Given an array of integers and a target nums taget, and you find the target value of the two integers in the array, 
and return to their array subscript.

You can assume that each input corresponds to only one answer. However, you can not re-use the same array element.

 

Resolution:

    Actual here is to give you a list of numbers to give you an expected, let you return

This list which two numbers equal to the expected number of subscript.

 

Code ideas:

    1. Direct use two for loops to traverse the list,

    2. a for loop from the first element, a for loop is subtracted from this element to traverse the list inside

    3. Then to judge the two elements is equal to the sum of the expected taget and, if equal, direct return to the standard element.

4. There may return more than we first set the default selection.

 

Implementation code:

 

class Solution():
def twoSun(self,nums,target):
for i in range(len(nums)):
for j in range(len(nums)):
if nums[i]+nums[j] ==target and i !=j:
return [i,j]

 

So let's take a simple test to test:

if __name__=="__main__":
solution=Solution()
print(solution.twoSun([1,2,3,4,5,6],5))

Execute print the results:

 

 

 

So I look at whether a given list which is right.

We know that the python list is counting from 0, the next target is the list of elements inside the first element of 0, and then once on.

We can see that array element at index 0 and 1 are respectively 3 and 4, 4 + 5 =

In line with expectations, think through the code.

 

Let's do the next unit testing:

class TestCase(unittest.TestCase):
def setup(self):
pass
def tearDown(self) -> None:
pass
def testcaseone(self):
self.solution = Solution()
result=self.solution.twoSun([1, 2, 3, 4, 5, 6], 5)
self.assertEqual(result,[0,3])
def testcasetow(self):
self.solution = Solution()
result = self.solution.twoSun(["1", 2, 3, 4, "5", 6], "5")
self.assertEqual(result, [0, 3])
def testcasethree(self):
self.solution = Solution()
result = self.solution.twoSun(["a", 2, 3, 4, "b", 6], "ab")
self.assertEqual(result, False)
if __name__=="__main__":

unittest.main()

 

 

 

After the test code runs, we found the mistake. In fact, we do not have control of the parameters, then we have to upgrade the code,

class Solution():
def twoSun(self,nums:list,target:int):
for i in range(len(nums)):
for j in range(len(nums)-i):
try:
if nums[i]+nums[j] ==target and i !=j:
return [i,j]
except:
return False

operation result

 

 

 

Therefore, in the testing process, we must be certain of our unit test code. The sooner unit testing done, the sooner we discovered the problem. Write your own code must be tested.

 

Guess you like

Origin www.cnblogs.com/leiziv5/p/11750540.html