Python fun little problem - to find out the number appears more than half the length of the array elements

[This article comes days outside the park owned by a cloud of blog]

Utilization of universality and particularity to solve, as follows:

import unittest
from datetime import datetime


class GetFreqNumbersFromList(unittest.TestCase):
    def setUp(self):
        print("\n")
        self.start_time = datetime.now()
        print(f"{self._testMethodName} start: {self.start_time}")

    def tearDown(self):
        self.end_time = datetime.now()
        print(f"{self._testMethodName} end: {self.end_time}")
        exec_time = (self.end_time - self.start_time) .microseconds
         Print (F " {} exec_time self._testMethodName: exec_time {} " ) 

    DEF normal_solution (Self, the _list, _DEBUG = False):
         "" " 
        universal solution 
        using a dictionary record the number of times each element appears - and then find out the number of elements that appear more than half the length of the array elements in 
        the case of universal solution for any number of statistics apply without light just over half the length of the array for the number of occurrences of 
        "" " 
        _target = len (_list ) // 2 
        _dict = {}
         for _member in the _list:
             IF _member Not  in _dict:
                _dict.setdefault (_member,  . 1)
             the else : 
                _dict [_member] + =. 1 
        _RET = [_member for _member in _dict IF _dict [_member]> _target]
         IF _DEBUG:
             Print (_RET)
         return _RET 

    DEF specific_solution (Self, the _list, _DEBUG = False):
         "" " 
        particularity Solution 
        suppose there are two elements often appear in more than half the length of the array would come to number two elements appear contradictory results beyond the length of the array - so more than half the length of the array elements is the only the 
        sorted in the middle of the array must be the target solution 
        particularity solution can only half the number of times over the length of the array elements appear for 
        "" " 
        _list.sort () 
        IF _debug:
            print(_list[len(_list) // 2])
        return _list[len(_list) // 2]

    def test_normal_solution(self):
        actual_result = self.normal_solution([2,2,2,2,2,2,1,1,1,1,1], False)
        self.assertEqual(actual_result[0], 2)

    def test_specific_solution(self):
        actual_result = self.specific_solution([2,2,2,2,2,2,1,1,1,1,1], False)
        self.assertEqual(actual_result, 2)


if __name__ == "__main__":
     #Identify the number of occurrences of more than half the length of the array element 
    Suite = unittest.TestSuite () 
    suite.addTest (GetFreqNumbersFromList ( ' test_normal_solution ' )) 
    suite.addTest (GetFreqNumbersFromList ( ' test_specific_solution ' )) 
    Runner = unittest.TextTestRunner () 
    runner.run ( suite)

Test Results:

 

In the micro-channel public number programmed to send an article on this issue see LeetCode, write about yourself ♪ (· ω ·) Techno

Guess you like

Origin www.cnblogs.com/LanTianYou/p/12156811.html