Brush Title: determining whether there is a repeating element

Topic:

Given an array of integers, it determines whether there is a repeating element. 
If no value appears at least twice in the array, the function returns true.
If each element of the array is not the same, false is returned.

Analysis: Given the array, the array has a repeating element, it returns true, did not return false, which is typical of a list of questions and for cycling. Ideas: for looping through List, and the first to judge whether there is remaining equal, the presence of repetitive elements represent equal, it returns true, otherwise returns false.

    We look at how to achieve python version, code is as follows:

    

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

  

 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()

  

    Test Results

Test code coverage:

    python versions or not particularly difficult to achieve, easier to understand, we are going to look at how the java version is implemented?

    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;
    }


}

  

   Test code:
public class FourTest {

    @Test
    public void testFind() {
        Four four=new Four();
        List<Integer> list=new ArrayList<Integer>();
        boolean rest=four.find(list);
        assertFalse(rest);
    }
    @Test
    public void testFind2() {
        Four four=new Four();
        List<Integer> list=new ArrayList<Integer>();
        list.add(0);
        boolean rest=four.find(list);
        assertFalse(rest);
    }
    @Test
    public void testFind3() {
        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() {
        Four four=new Four();
        List<Integer> list=new ArrayList<Integer>();
        list.add(0);
        list.add(1);
        list.add(0);
        boolean rest=four.find(list);
        assertTrue(rest);
    }
}

 



  Test Results:

    Code coverage:

     We are two versions of python and java implementation can be seen, in fact, come out to solve some problems, not so difficult, the key is that ideas, and not have a reasonable idea to solve this problem. With a good idea, but also to achieve with the code to realize their ideas, after implementation, the corresponding increase in check, and auxiliary test code for testing.

      

     In the actual code, we want to do more tests, because the bottom of the lowest cost of test changes.

 

     No public concern, sons of thunder, said testing for more premium content pushed to you.

 

Guess you like

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