zoukankan      html  css  js  c++  java
  • Python习题持续更新

    1、你是一个高级测试工程师,现在要做性能测试,需要你写一个函数,批量生成一些注册使用的账号。

    产生的账号是以@163.com结尾,长度由用户输入,产生多少条也由用户输入,用户名不能重复,用户名必须由大写字母、小写字母、数字组成,结果如下图:

    import random
    import string
    
    def submit_data(le,num):
        data_list = []
        if le>=6 and le<=10:
            while len(data_list)<num:
                data_lower=random.sample(string.ascii_lowercase,2)
                data_upper=random.sample(string.ascii_uppercase,2)
                data_digit=random.sample(string.digits,le-2-2)
                data=''.join(data_upper+data_lower+data_digit)
                new_data=data+'@163.com'+'
    '
                if new_data not in data_list:
                    data_list.append(new_data)
            with open('a.txt','w+', encoding='utf8') as fw:
                fw.writelines(data_list)
        else:
            print("请重新输入,长度必须大于6")
    
    
    submit_data(7,8)

    2、测试机器的磁盘太小,经常报警,要写一个清理日志的脚本,每次运行就把三天之前的日志删除,日志名的格式是xxx-20170623.log。

    def get_datetime(day,format='%Y%m%d'):
        import datetime,time
        res=datetime.datetime.now()+datetime.timedelta(day)
        res_time=res.strftime(format)
        return res_time
    
    def log_remove(logpath,day):
        import os
        file_list=os.listdir(logpath)
        three_day = get_datetime(day)
        for log in file_list:
            if log.endswith('.log'):
                logdate=log.split('.')[0].split('-')[1]
                if logdate<=three_day:
                    os.remove(logpath+os.path.sep+log)
    
    
    log_remove(r'D:个人log',-3)

    3、公司服务器,经常被别人攻击,要写个监控nginx日志的脚本,每分钟运行一次,如果这一分钟内同一个ip请求次数超过200次,加入黑名单,nginx日志每一行的格式如下:

    1
    2
    3
    46.161.9.44 - - [23/Jun/2017:03:17:37 +0800] "GET /bbs/forum.php?mod=forumdisplay&fid=2 HTTP/1.0" 200 48260 "http://aaaa.bbbbb.com/bbs/forum.php?mod=forumdisplay&fid=2" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36" "-"
    46.161.9.44 - - [23/Jun/2017:03:17:39 +0800] "GET /bbs/forum.php?mod=forumdisplay&fid=2 HTTP/1.0" 200 46200 "http://aaaa.bbbbb.com/bbs/forum.php?mod=forumdisplay&fid=2" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36" "-"
     

    4、额,来个小插曲,写程序求出1-100的累加和。1+2+3+4+5+.....+100。

    sum=0
    for i in range(1,101):
        sum+=i
    
    print(sum)

    5、不知不觉你已经写了很多python代码了,代码全部都放在e盘下面的my_code文件夹中,突然突发奇想,想统计了一下总共写了多少行代码,包括空行和注释,要把代码的行数、空行数、注释行数都统计出来。

    6、有一个文件,里面有一些敏感词汇,如下,如果输入这些词,就用**代替,然后输出,例如输入今天没吃饭,碰到一个傻逼,原来那个sb是小明。输出今天没吃饭,碰到一个**,原来那个**是小明。

     
    1
    2
    3
    4
    5
    6
    7
    傻逼
    b
    煞笔
    煞比
    sb
    B
    shabi

    7、写个简单的爬虫,把这个链接http://so.fengniao.com/index.php?action=Image&keyword=%E7%A7%81%E6%88%BF,里面所有的图片保存到本地。

    8、有一个存着学生成绩的文件,里面存的是json串,json串读起来特别不直观,需要你写代码把它都写到excel中,并计算出总分和平均分,json格式如下

     
    1
    2
    3
    4
    5
    {
    "1":["小花",99,100,98.5],
    "2":["小王",90,30.5,95],
    "3":["小明",67.5,49.6,88]
    }

    写完之后excel格式如下:

    9、列表、元组、集合有什么区别?

    list=[]

    tuple=()  元组的值不能改变

    set=set()

    11、怎么通过不引入第三方变量的方式,交换两个变量的值?

    a=1
    b=2
    a,b=b,a
     

    12、下面的代码执行结果是什么,为什么?

     
    li = [1,1,2,3,4,5,6,7,8,9]
    for i in li:
        if i%2!=0:
            li.remove(i)
    print(li)

    [1, 2, 4, 6, 8]

    13、下面这段代码的执行结果是什么?

    money = 500
    def test(consume):
        
        return money - consume
     
    def test1(money):
     
        return test(money)+money
     
    money = test1(money)
    print(money)

    500

    14、下面这段代码会打印什么,为什么?

    def test():
        global a
        a = 5
    
    def test1():
        c = a + 5
        return c
    
    res = test1()
    print(res)

     报错,因为test1中a未定义

     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    ok_req={
        "version": "9.0.0",
        "is_test": True,
        "store": "",
        "urs": "",
        "device": {
            "os": "android",
            "imei": "99001062198893",
            "device_id": "CQliMWEyYTEzNTYyYzk5MzJmCTJlNmY3Zjkx",
            "mac": "02:00:00:00:00:00",
            "galaxy_tag": "CQliMWEyYTEzNTYyYzk5MzJmCTJlNmY3Zjkx",
            "udid": "a34b1f67dd5797df93fdd8b072f1fb8110fd0db6",
            "network_status": "wifi"
        },
        "adunit": {
            "category": "VIDEO",
            "location": "1",
            "app": "7A16FBB6",
            "blacklist": ""
        },
        "ext_param":{
         "is_start" : 0,
         "vId":"VW0BRMTEV"
        }
    }
    not_ok={
    "version": "9.0.0",
    "is_test": True,
    "urs": "",
    "store": "",
    "device": {
    "os": "android",
    "imei": "99001062298893",
    "device_id": "CQliMWEyYTEzNTYyYzk5MzJmCTJlNmY3Zjkx",
    "mac": "02:00:00:00:00:00",
    "galaxy_tag": "CQliMWEyYTEzNTYyYzk5MzJmCTJlNmY3Zjkx",
    "udid": "a34b1f67dd5797da93fdd8b072f1fb8110fd0db6",
    "network_status": "wifi"
    },
    "adunit": {
    "category": "VIDEO",
    "location": "1",
    "app": "7A16FBB6",
    "blacklist": ""
    },"ext_param": {
    "is_start": 0,
    "vid": "VW0BRMTEV"
    }
    }

    15、上面有两个字典,请写一个函数打印两个字典中不一样的key和value

    16、现有字符串a="Im Love python",需要变成b="python love Im"

    a='Im Love python'
    b=a.split(' ')
    c=' '.join(b[::-1])
    print(c)
    17、 将"wdnwjfwj we hd 9#sdfekfmew 2011"里的数字取出来相加结果为2020
     
    s='wdnwjfwj we hd 9#sdfekfmew 2011'
    s1=s.split('#')
    first_num=s1[0].split(' ')[-1]
    end_num=s1[1].split(' ')[-1]
    fe=int(first_num)+int(end_num)
  • 相关阅读:
    iaas,paas,saas理解
    July 06th. 2018, Week 27th. Friday
    July 05th. 2018, Week 27th. Thursday
    July 04th. 2018, Week 27th. Wednesday
    July 03rd. 2018, Week 27th. Tuesday
    July 02nd. 2018, Week 27th. Monday
    July 01st. 2018, Week 27th. Sunday
    June 30th. 2018, Week 26th. Saturday
    June 29th. 2018, Week 26th. Friday
    June 28th. 2018, Week 26th. Thursday
  • 原文地址:https://www.cnblogs.com/niuniu2018/p/8270970.html
Copyright © 2011-2022 走看看