难题:
键入一个数组,輸出该数组的第二大的数据,而且撰写有关的功能测试

留意:

1.假如list带有非int, float元素必须remove
2.假如list有反复的较大 元素,必须自身解决,内嵌的

list.sort(reverse=True)
heapq.nlargest
排序,元素数量不会改变。

另附编码

removeInvalidItems 去掉并不是int或float种类的值。
留意:不可以像下面那样用一次循环系统,由于remove某一元素,字符发生了更改,一些值并不可以清除

for item in l:  # remove non_value items
    if not isinstance(item, (int, float)):
        l.remove(item)

下面是能用编码,文件夹名称为 findSecondUtil.py

def removeInvalidItems(l):
    tmpl = list()
    for item in l:
        if not isinstance(item, (int, float)):
            tmpl.append(item)
    for item in tmpl:
        l.remove(item)
    return l

findSecondItem.py 完成寻找第二数字的第一种方式。这类方式不用去掉反复元素。

import sys
from hyang.python3.interview.boyan.findSecondUtil import removeInvalidItems
#from findSecondUtil import removeInvalidItems
def findSecond(l):
    l = removeInvalidItems(l)
    length = len(l)
    if length == 0:
        print("there is no number item in the list")
        return None
    elif length == 1:
        print("there is only one number item, it's ", l[0])
        return None
    elif length > sys.maxsize:
        print("out of scope")
        return None
    largest, second = max(l), min(l)
    if largest == second:
        return None
    for item in l:
        if item > second and item < largest:
            second = item
    return second

findSecondTest.py 测试程序

import unittest
from hyang.python3.interview.boyan.findSecondItem import findSecond
#from findSecondItem import findSecond
class Test_findSecondItem(unittest.TestCase):
    # 假如跑全部测试用例,只运作一次必要条件和完毕标准。则用setupclass()和teardownclass()
    @classmethod
    def setUpClass(cls):
        print("在全部的功能测试实行以前,只实行一次============")
    @classmethod
    def tearDownClass(cls):
        print("在全部的功能测试实行以后,只实行一次============")
    # empty list
    def test_findSecondItem_01(self):
        l1 = list()
        assert (findSecond(l1) == None)
    # one item in list
    def test_findSecondItem_02(self):
        l1 = [2]
        assert (findSecond(l1) == None)
    # No item in list after remove non-number items
    def test_findSecondItem_03(self):
        l1 = [None, "abc", "xyz"]
        assert (findSecond(l1) == None)
    # one item in list after remove non-number items
    def test_findSecondItem_04(self):
        l1 = [None, 3, "abc"]
        assert (findSecond(l1) == None)
    # duplated largest number
    def test_findSecondItem_05(self):
        l1 = [32, None, 12, "abc", 8, 6, 36, 3, 32, 4, 36, 9, 25, '35', 36]
        assert (findSecond(l1) == 32)
# python3中写不写都是会实行的
if __name__ == '__main__':
    unittest.main()

方式二:findSecondNum.py 从目录去掉全部的较大 元素,再在目录中找一个较大 便是第二大元素。

import sys
from hyang.python3.interview.boyan.findSecondUtil import removeInvalidItems
def findSecond(l):
    l = removeInvalidItems(l)
    length = len(l)
    if length == 0:
        print("there is no number item in the list")
        return None
    elif length == 1:
        print("there is only one number item, it's ", l[0])
        return None
    elif length > sys.maxsize:
        print("out of scope")
        return None
    largest=max(l)
    largest_count=l.count(largest)
    while largest_count>0:  #remove all the largest item
        l.remove(largest)
        largest_count-=1
    if len(l)==0:
        return None
    else:
        return max(l)

  

方式三:运用内嵌的list.sort,可是要去掉反复元素

import sys
from hyang.python3.interview.boyan.findSecondUtil import removeInvalidItems
def findSecond(l):
    removeInvalidItems(l)
    l2 = list(set(l))   # remove duplicated items
    l2.sort(reverse=True)
    length=len(l2)
    if length>=2:
        return l2[1]
    else:
        return None

方式四:与方式三相近,运用内嵌的heapq.nlargest,也必须去掉反复元素

import sys, heapq
from hyang.python3.interview.boyan.findSecondUtil import removeInvalidItems
def findSecond(l):
    removeInvalidItems(l)
    l2 = list(set(l))   # remove duplicated items
    length = len(l)
    length = len(l2)
    if length >= 2:
        return heapq.nlargest(2, l2)[1]
    else:
        return None

最终必须留意程序执行所属途径,见下面的图,能够融合自身的配备来调节。

文章来源于网络,如有侵权请联系站长QQ61910465删除
本文版权归趣快排SEO www.SeogurUblog.com 所有,如有转发请注明来出,竞价开户托管,seo优化请联系QQ▶61910465