Python 面试题:输入一个数组,输出该数组的第二大的数字
問題:
輸入一個數(shù)組,輸出該數(shù)組的第二大的數(shù)字,并且編寫相關(guān)的測試用例
注意:
1.如果list含有非int, float元素需要remove
2.如果list有重復(fù)的最大元素,需要自己處理,內(nèi)置的list.sort(reverse=True)和heapq.nlargest排序,元素個數(shù)不變。
附上代碼
removeInvalidItems 去掉不是int或float類型的值。
注意:不能像下邊這樣用一次循環(huán),因為remove某個元素,下標(biāo)發(fā)生了改變,有些值并不能移除
下邊是可用代碼,文件名為 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 lfindSecondItem.py 實現(xiàn)找到第二大數(shù)字的第一種方法。這種方法不用去掉重復(fù)元素。
''' 遇到問題沒人解答?小編創(chuàng)建了一個Python學(xué)習(xí)交流QQ群:778463939 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學(xué)習(xí)教程和PDF電子書! ''' import sys from hyang.python3.interview.boyan.findSecondUtil import removeInvalidItems #from findSecondUtil import removeInvalidItemsdef findSecond(l):l = removeInvalidItems(l)length = len(l)if length == 0:print("there is no number item in the list")return Noneelif length == 1:print("there is only one number item, it's ", l[0])return Noneelif length > sys.maxsize:print("out of scope")return Nonelargest, second = max(l), min(l)if largest == second:return Nonefor item in l:if item > second and item < largest:second = itemreturn secondfindSecondTest.py 測試代碼
import unittest from hyang.python3.interview.boyan.findSecondItem import findSecond #from findSecondItem import findSecondclass Test_findSecondItem(unittest.TestCase):# 如果跑所有用例,只運行一次前提條件和結(jié)束條件。則用setupclass()和teardownclass()@classmethoddef setUpClass(cls):print("在所有的測試用例執(zhí)行之前,只執(zhí)行一次============")@classmethoddef tearDownClass(cls):print("在所有的測試用例執(zhí)行之后,只執(zhí)行一次============")# empty listdef test_findSecondItem_01(self):l1 = list()assert (findSecond(l1) == None)# one item in listdef test_findSecondItem_02(self):l1 = [2]assert (findSecond(l1) == None)# No item in list after remove non-number itemsdef test_findSecondItem_03(self):l1 = [None, "abc", "xyz"]assert (findSecond(l1) == None)# one item in list after remove non-number itemsdef test_findSecondItem_04(self):l1 = [None, 3, "abc"]assert (findSecond(l1) == None)# duplated largest numberdef 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中寫不寫都會執(zhí)行的 if __name__ == '__main__':unittest.main()方法二:findSecondNum.py 從列表去掉所有的最大元素,再在列表中找一個最大就是第二大元素。
''' 遇到問題沒人解答?小編創(chuàng)建了一個Python學(xué)習(xí)交流QQ群:778463939 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學(xué)習(xí)教程和PDF電子書! ''' import sys from hyang.python3.interview.boyan.findSecondUtil import removeInvalidItemsdef findSecond(l):l = removeInvalidItems(l)length = len(l)if length == 0:print("there is no number item in the list")return Noneelif length == 1:print("there is only one number item, it's ", l[0])return Noneelif length > sys.maxsize:print("out of scope")return Nonelargest=max(l)largest_count=l.count(largest)while largest_count>0: #remove all the largest iteml.remove(largest)largest_count-=1if len(l)==0:return Noneelse:return max(l)方法三:利用內(nèi)置的list.sort,但是要去掉重復(fù)元素
import sys from hyang.python3.interview.boyan.findSecondUtil import removeInvalidItemsdef findSecond(l):removeInvalidItems(l)l2 = list(set(l)) # remove duplicated itemsl2.sort(reverse=True)length=len(l2)if length>=2:return l2[1]else:return None方法四:與方法三類似,利用內(nèi)置的heapq.nlargest,也需要去掉重復(fù)元素
''' 遇到問題沒人解答?小編創(chuàng)建了一個Python學(xué)習(xí)交流QQ群:778463939 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學(xué)習(xí)教程和PDF電子書! ''' import sys, heapq from hyang.python3.interview.boyan.findSecondUtil import removeInvalidItemsdef findSecond(l):removeInvalidItems(l)l2 = list(set(l)) # remove duplicated itemslength = len(l)length = len(l2)if length >= 2:return heapq.nlargest(2, l2)[1]else:return None最后需要注意執(zhí)行程序所在路徑,見下圖,可以結(jié)合自己的配置來調(diào)整。
總結(jié)
以上是生活随笔為你收集整理的Python 面试题:输入一个数组,输出该数组的第二大的数字的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python函数的万能参数
- 下一篇: python关于列表增加元素的几种操作