2019python面試題-有1、2、3、4個數字,能組成多少個互不相同且無重復數字的三位數


 

有1、2、3、4個數字,能組成多少個互不相同且無重復數字的三位數?都是多少?

 

方法一:

 

# python 3

# 最簡單方法 print ([(xyz)
for x in range(1,5) for y in range(1,5) for z in range(1,5) if ((x != y) and (y != z) and (x != z))]) # 運行結果 [(1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 3, 4), (1, 4, 2), (1, 4, 3), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3),
(3, 1, 2), (3, 1, 4), (3, 2, 1), (3, 2, 4), (3, 4, 1), (3, 4, 2), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (4, 3, 2)] # 詳細步驟
def num_range(): i
= 0 for x in range(1,5): for y in range(1,5): for z in range(1,5): if x != y & y != z & z !=x: i += 1 print('%d%d%d' % (x,y,z), end=' ') print('共有結果:%s個'%i) return i # 運行結果 123 124 132 134 142 143 213 214 231 234 241 243 312 314 321 324 341 342 412 413 421 423 431 432 共有結果:24個

 

方法二:

# 使用pop()

def num_pop(n1, n2, n3, n4):
    n_list = [n1,n2,n3,n4]
    count = 0
    for x in range(len(n_list)):
        nn_list = n_list.copy() # 每個for循環都必須copy一次列表
        a = str(nn_list.pop(x))
        for y in range(len(nn_list)):
            nnn_list = nn_list.copy()
            b = str(nnn_list.pop(y))
            for z in range(len(nnn_list)):
                print(a+b+str(nnn_list[z]), end = ' ')
                count +=1
    print('最終結果:%s個' % count)
    return n_list
num_pop(1,3,6,4)

運行結果:
136 134 163 164 143 146 316 314 361 364 341 346 613 614 631 634 641 643 413 416 431 436 461 463 最終結果:24個

# 使用remove()
def num_remove(n1, n2, n3, n4): n_list
= [n1, n2, n3, n4] count = 0 for x in n_list: nn_list = n_list.copy() nn_list.remove(x) a = str(x) for y in nn_list: nnn_list = nn_list.copy() nnn_list.remove(y) b = str(y) for z in nnn_list: print(a+b+str(z),end = ' ') count += 1 print('最終結果:%s個' % count) return n_list num_remove(1,2,4,5)

運行結果:
124 125 142 145 152 154 214 215 241 245 251 254 412 415 421 425 451 452 512 514 521 524 541 542 最終結果:24個

 

淺拷貝

l1 = [1,2,3,[1,2,3]]
l2 = l1.copy()

#第一層
l1.append('a')
print(l1,l2)           #[1, 2, 3, [1,2,3], 'a'] [1, 2, 3, [1,2,3]]
print(id(l1),id(l2))       #1905136061128 1905136005768
#id內存地址不一樣,創建了兩個地址空間,一個改變,另一個不會變化

# 第二層
l1[3][1] = 4
print(l1,l2)          #[1, 2, 3, [1, 4, 3],'a'] [1, 2, 3, [1, 4, 3]]
print(id(l1),id(l2))       #1905136061128 1905136005768
# id地址各自不變,但嵌套拷貝時,l1、l2都變化

深拷貝

# 對於深copy來說,兩個是完全獨立的,改變任意一個的元素(無論是多少層),另一個絕不會改變。
>>> import copy
>>> l1 = [1,2,3,[1,2,3]]
>>> l2 = copy.deepcopy(l1)
>>> id(l1)
1905136060616
>>> id(l2)
1905136060808
>>> l1.append('a')
>>> l1
[1, 2, 3, [1, 2, 3], 'a']
>>> l2
[1, 2, 3, [1, 2, 3]]
>>> id(l1) # id不變
1905136060616
>>> id(l2) # id不變
1905136060808
>>> l1[3][1] = 4
>>> l1 # 深拷貝不變
[1, 2, 3, [1, 4, 3], 'a']
>>> l2 # 深拷貝不變
[1, 2, 3, [1, 2, 3]]
>>> id(l1)
1905136060616
>>> id(l2)
1905136060808
>>> 

 

方法三:

# 有最少四個數字,隨機組成三位數
def num_remove_many(n1, n2, n3, n4, *args):
    n_list = [n1, n2, n3, n4]
    for var in args:
        n_list.append(var)
    count = 0
    for x in n_list:
        nn_list = n_list.copy()
        nn_list.remove(x)
        a = str(x)
        for y in nn_list:
            nnn_list = nn_list.copy()
            nnn_list.remove(y)
            b = str(y)
            for z in nnn_list:
                print(a+b+str(z),end = ' ')
                count += 1
    print('最終結果:%s個' % count)
    return n_list
num_remove_many(1,2,4,5,3)
        
運行結果:
124 125 123 142 145 143 152 154 153 132 134 135 214 215 213 241 245 243 251 254 253 231 234 235 412 415 413 421 425 423 451 452
453 431 432 435 512 514 513 521 524 523 541 542 543 531 532 534 312 314 315 321 324 325 341 342 345 351 352 354 最終結果:60個

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM