This is the 13th day of my participation in the August Essay Challenge.

Given an array of integers, determine whether there are duplicate elements. The function returns true if any value appears in the array at least twice. Return false if each element in the array is different.Copy the code

Given an array with duplicate elements, return true, no return false. This is typical with list and for loops. The for loop iterates through the list to determine if the first and the rest of the list are equal, and returns true, otherwise returns false.

Let’s look at the python version and see the code below:

def listfind(nums:list) - >int:
    for i in range(len(nums)):
        for j in range(len(nums))[i:]:
            if nums[j]==nums[i] andj! =i:return True
    return False
Copy the code

Test code:

import  unittest
from four import listfind
class TestCae(unittest.TestCase) :
    def setUp(self) - >None:
        pass
    def tearDown(self) - >None:
        pass
    def testone(self) :
        reslt=listfind([])
        self.assertFalse(reslt)
    def testtwo(self) :
        reslt=listfind([1])
        self.assertFalse(reslt)
    def testthree(self) :
        reslt=listfind([1.1])
        self.assertTrue(reslt)
if __name__=="__main__":
    unittest.main()
Copy the code

Let’s look at the result of the execution

Test code coverage:

The Python version of the implementation is not particularly difficult, it is relatively easy to understand, let’s see how the Java version of the implementation?
Implementation code:
public class Four {
    public boolean find(List<Integer> list) {
        for (int a = 0; a < list.size(); a++) {
            for (int b = a; b < list.size(); b++) {
                if (list.get(a).equals(list.get(b)) && !new Integer(a).equals(new Integer(b))) {
                    return true; }}}return false; }}Copy the code

Let’s write the specific test code


public class FourTest {

    @Test
    public void testFind(a) {
        Four four=new Four();
        List<Integer> list=new ArrayList<Integer>();
        boolean rest=four.find(list);
        assertFalse(rest);
    }
    @Test
    public void testFind2(a) {
        Four four=new Four();
        List<Integer> list=new ArrayList<Integer>();
        list.add(0);
        boolean rest=four.find(list);
        assertFalse(rest);
    }
    @Test
    public void testFind3(a) {
        Four four=new Four();
        List<Integer> list=new ArrayList<Integer>();
        list.add(0);
        list.add(0);
        boolean rest=four.find(list);
        assertTrue(rest);
    }
    @Test
    public void testFind4(a) {
        Four four=new Four();
        List<Integer> list=new ArrayList<Integer>();
        list.add(0);
        list.add(1);
        list.add(0);
        booleanrest=four.find(list); assertTrue(rest); }}Copy the code

Test results:

Code coverage:

Through the implementation of Python and Java, we can see that some problems are not so difficult to solve, the key lies in the idea, and there is no reasonable idea to solve this problem. Have a good idea, but also to achieve, with the code to achieve their own ideas, after implementation, increase the corresponding verification, and auxiliary test code for testing.

In real code, we do more testing, because the lower level of testing changes are the least expensive.