zoukankan      html  css  js  c++  java
  • 实战一道2级综合编程模拟题

    请编写程序,生成随机密码。具体要求如下:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬

    (1)使用 random 库,采用 0x1010 作为随机数种子。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬

    (2)密码 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&* 中的字符组成。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬

    (3)每个密码长度固定为 10 个字符。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬

    (4)程序运行每次产生 10 个密码,每个密码一行。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬

    (5)每次产生的 10 个密码首字符不能一样。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬

    (6)程序运行后产生的密码保存在“随机密码.txt”文件中。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬

     ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬

    不写入文件,直接 print 10 个密码即可自动评阅

    解析:

    通读题目要求,req5是个难点,如何保证产生的10个密码首字符不同?
    一个自然的想法是记录并比较
    进而对于req4,就不能用for循环固定循环次数,需要用while循环
    while循环退出循环的依据是什么?取决于如何记录密码,比如把每次生成的密码记录到一个列表,用len(ls)<10做退出循环的条件
    每次生成的密码设为cipher,考虑以及几点
    1.如何生成2.如何满足req5.3如何输出
    1.如何生成,可以考虑用s[random.randint(0,len(s)-1)]或random.choice(s), s 是字符集
    2.如何满足req5,可以记录每个cipher的首字符,并记录在字符串execlude,只有满足的才添加到ls中
    3. 输出,每个密码一行,cipher+ ' '就可以了

    具体代码如下:

    import random
    
    fo = open('D:/bak/skydrive/automation/2级/execise/随机密码.txt','w')
    random.seed(0x1010)
    s = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*'
    cipher = '' # store a cipher,store it into ls and then clear it
    ls = [] # store all 10 cipher, len(ls) to count the num of cipher
    execlude = '' # store the first char of a cipher,make sure the cipher meet req#5
    while len(ls) < 10: # generate 10 cipher, not use for loop as due to req#5, the running time is not predictable
        cipher = '' # reset cipher each round
        for i in range(10): # 10个字符
            #cipher += random.choice(s) # pickup a random char from s by random.choice and then connect cyper by +
            cipher += s[random.randint(0,len(s)-1)]        
        cipher = cipher + '
    '
        if cipher[0] in execlude:
            continue
        else:
            ls.append(cipher)
            execlude += cipher[0]
            #fo.writelines(cipher) # if write line by line,the performance is somewhat poor,let write the ls to file directly
    #print(c)
    print(''.join(ls))
    ##for line in ls:
    ##    fo.write(line)
    fo.write(''.join(ls))
    #print(execlude)
    fo.close()
    
        

    执行结果:

    So2WpkoC7i
    armJ86eUG9
    B*GcqsYC^B
    wQ3bcfcAJy
    Xdyg8pQTIS
    YO!1YH1AP3
    cuhZUk@s5&
    D@4d9$TBfp
    TBm#WfYNHr
    Ue75y$E9Cv

    由此,可见2级综合编程题,需要在较短时间内,能解读问题,综合应用各种基本编程技能,完成题目要求,对初学者还是有一定难度的。

    需要仔细审题,吃透题意,提高基本数据处理的熟练度,才能从容过关。

    注意,设置seed的目的是为了产生的随机数可以重复,本以为可以用sample(pop,k) 直接生成10个密码,实践发现,此方法产生的密码和单个随机产生的不一样

    >>> random.seed(0x1010)
    >>> lt = random.sample(s,10)
    >>> lt
    ['S', 'o', '2', 'W', 'p', 'k', '&', '!', '@', '^']

    对比,

    So2WpkoC7i

    可见,前6个字符是一样的,后面的不一致,原因不明。

  • 相关阅读:
    LeetCode 326. Power of Three
    LeetCode 324. Wiggle Sort II
    LeetCode 322. Coin Change
    LeetCode 321. Create Maximum Number
    LeetCode 319. Bulb Switcher
    LeetCode 318. Maximum Product of Word Lengths
    LeetCode 310. Minimum Height Trees (DFS)
    个人站点大开发!--起始篇
    LeetCode 313. Super Ugly Number
    LeetCode 309. Best Time to Buy and Sell Stock with Cooldown (DP)
  • 原文地址:https://www.cnblogs.com/jamesxu/p/11079893.html
Copyright © 2011-2022 走看看