python 输入一个数组,输出该数组的第二大的数字
今天一个外包面试题目,这下才看清楚要写测试用例,新手错误之处敬请谅解。
问题:输入一个数组,输出该数组的第二大的数字,并且编写相关的测试用例
注意:
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
最后需要注意执行程序所在路径,见下图,可以结合自己的配置来调整。