Python:1-8個數字中任意4個組合
阿新 • • 發佈:2021-12-14
測試目的
1-8通道取n個數組合,測試不同組合場景執行邏輯正確性
實現1:列舉法
1 def test_loadTips_TwoChannels(): 2 """ 3 1-8通道任意2通道組合扎取 4 """ 5 # 方法一:列舉法 6 for a2 in range(1, 9): 7 for b2 in range(a2 + 1, 9): 8 load_tips({"Sequence": "Lane7TipsPos1", "Channels": [a2, b2], "IfIncrement": False})9 unload_tips({"Sequence": "Lane7TipsPos1", "Channels": [a2, b2], "IfIncrement": True}) 10 reload_sequence("Lane7TipsPos1") 11 12 13 test_loadTips_TwoChannels()
實現2:Python標準庫itertools
1 def test_loadTips_ThreeChannel(): 2 """ 3 1-8通道任意3通道組合扎取 4 """ 5 # 方法二:Python標準庫itertools6 channel_list = [1, 2, 3, 4, 5, 6, 7, 8] 7 for c in itertools.combinations(channel_list, 3): 8 threeChannel = map(int, c) 9 load_tips({"Sequence": "Lane1-TipSequence", "Channels": threeChannel, "IfIncrement": False}) 10 unload_tips({"Sequence": "Lane1-TipSequence", "Channels": threeChannel, "IfIncrement": True}) 11 reload_sequence("Lane1-TipSequence") 12 13 14 test_loadTips_ThreeChannel()
實現3:遞迴
1 # *****************************************case:1-8通道7通道組合扎取正確***************************************** 2 def test_loadTips_SevenChannel(): 3 """ 4 1-8通道7通道組合扎取 5 :return: 6 """ 7 # 方法三:遞迴法 從8個數中取7個數,不重複的,共計28種組合 8 channel_list = [1, 2, 3, 4, 5, 6, 7, 8] 9 10 def combine(channel_list, l): 11 result = [] 12 tmp = [0] * l 13 length = len(channel_list) 14 15 def next_num(li=0, ni=0): 16 if ni == l: 17 result.append(copy.copy(tmp)) 18 return 19 for lj in range(li, length): 20 tmp[ni] = channel_list[lj] 21 next_num(lj + 1, ni + 1) 22 23 next_num() 24 return result 25 26 for channel in combine(channel_list, 7): 27 # print channel 28 load_tips({"Sequence": "Lane1-TipSequence", "Channels": channel, "IfIncrement": False}) 29 unload_tips({"Sequence": "Lane1-TipSequence", "Channels": channel, "IfIncrement": True}) 30 reload_sequence("Lane1-TipSequence") 31 32 33 test_loadTips_SevenChannel()