zoukankan      html  css  js  c++  java
  • 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个
  • 相关阅读:
    迭代器和生成器
    案例:复制大文件
    案例:使用seek倒查获取日志文件的最后一行
    Leetcode165. Compare Version Numbers比较版本号
    Leetcode137. Single Number II只出现一次的数字2
    Leetcode129. Sum Root to Leaf Numbers求根到叶子节点数字之和
    Leetcode116. Populating Next Right Pointers in Each Node填充同一层的兄弟节点
    Leetcode114. Flatten Binary Tree to Linked List二叉树展开为链表
    Leetcode113. Path Sum II路径总和2
    C++stl中vector的几种常用构造方法
  • 原文地址:https://www.cnblogs.com/yekushi-Z/p/11457739.html
Copyright © 2011-2022 走看看