Python第一次考试

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_44304481/article/details/86583763

1.对下面集合中随机取数字,要求取出的数字是3的倍数但不能是4的倍数,成功取到10个数字并对取到的所有数字求和输出

import random
# nums = list({5,10,3,8,6,10,9,15,24,30,27,48,24})
# count = 0
# total = 0
# while True:
#     num = random.choice(nums)
#     if num % 3 == 0 and num % 4 != 0:
#         total += num
#         print(num,total)
#         count += 1
#         if count >= 10:
#             break
# print(total)

2、选取文件选出含有ftp的链接,且文件类型是gz或者xz的文件,要求打印其文件名,文件名升序排列

# files_2 = [
# "ftp://ftp.astron.com/pub/file/file-5.14.tar.gz",
# "ftp://ftp.gmplib.org/pub/gmp-5.1.2/gmp-5.1.2.tar.xz",
# "ftp://ftp.vim.org/pub/vim/unix/vim-7.3.tar.bz2",
# "http://anduin.linuxfromscratch.org/sources/LFS/lfs-packages/conglomeration//iana-etc/iana-etc-2.30.tar.bz2",
# "http://anduin.linuxfromscratch.org/sources/other/udev-lfs-205-1.tar.bz2",
# "http://download.savannah.gnu.org/releases/libpipeline/libpipeline-1.2.4.tar.gz",
# "http://download.savannah.gnu.org/releases/man-db/man-db-2.6.5.tar.xz",
# "http://download.savannah.gnu.org/releases/sysvinit/sysvinit-2.88dsf.tar.bz2",
# "http://ftp.altlinux.org/pub/people/legion/kbd/kbd-1.15.5.tar.gz",
# "http://mirror.hust.edu.cn/gnu/autoconf/autoconf-2.69.tar.xz",
# "http://mirror.hust.edu.cn/gnu/automake/automake-1.14.tar.xz",
# ]
#
# ans_2 = []
# for name in files_2:
#     if name[:6]=="ftp://" and (name[-3:]==".xz" or name[-3:]==".gz"):
#         ans_2.append(name.split("/")[-1])
#
# print(sorted(ans_2))

3、转置矩阵

随机生成4*4的方阵所有数据,打印出来

在此矩阵上求转置矩阵并打印出来

# m = 5
# n = 5
# matrix = [[random.randint(1,20) for j in range(n)] for i in range(n)]
# print(*matrix,sep='\n')
# print('~~~~~~~~~~~~~~~~~~~~~~')
# for i in range(m):
#     for j in range(i):
#         matrix[i][j],matrix[j][i] = matrix[j][i],matrix[i][j]
# print(*matrix,sep='\n')

# 第二种实现

# import random
# matrix = []
# for i in range(4):
#     matrix.append([])
#     for j in range(4):
#         matrix[i].append(random.randint(1,1000))
# for i in range(4):
#     for j in range(4):
#         print(matrix[i][j],end="\t")
#     print()
# print("-"*30)
# newmatrix = []
# for i in range(4):
#     newmatrix.append([])
#     for j in range(4):
#         newmatrix[i].append(matrix[j][i])
# for i in range(4):
#     for j in range(4):
#         print(newmatrix[i][j],end="\t")
#     print()
# print("-"*30)

4、生成随机的20个ID

ID格式要求:

时间戳_三位随机数字号码_8位随机小写字母

1506571959_089_xxkeabef

datetime.datetime.now().timestamp() 返回的是时间戳,但是带微秒

# import datetime
import string
# from time import time
# s =str(int(datetime.datetime.now().timestamp()))
# for i in range(20):
#     d = random.randint(100,200)
#     c = random.sample(string.ascii_lowercase, 8)
#     print(s,d,c,sep = '_')

去除重复数字,并且升序打印

lst = [29, 30, 37, 22, 59, 75, 79, 41, 47, 68, 73, 27, 69, 64,  40, 28, 64]
lst = list(set(lst))
print(sorted(lst)) 

# 用列表去重复
# sum = [29, 30, 37, 22, 59, 75, 79, 41, 61, 75, 75, 78, 63, 52, 44, 20, 27, 29, 48, 66, 60, 36]
#
# length = len(sum)
# num = [0]*length
# for i in range(length-1):
#     if num[i] == 1:
#         continue
#     for j in range(i+1,length):
#         if num[j] == 1:
#             continue
#         if sum[i] == sum[j]:
#             num[j] = 1

# # target = [sum[i] for i in range(length) if not num[i]]	这是注释
# target = []
# for i in range(length):
#     if not num[i]:
#         target.append(sum[i])
#
# print(len(target),sorted(target))

6、排序

375 3.5 6 20 9 -20 68

对上面的数字实现冒泡排序和选择排序

 # 冒泡排序
# lst = [375, 3.5, 6, 20, 9, -20, 68]
# length = len(lst)
# for i in range(length):
#     for j in range(length-1):
#         if lst[j] > lst[j+1]:
#             lst[j],lst[j+1] = lst[j+1],lst[j]
# print(lst)

#  选择排序
# lst = [375, 3.5, 6, 20, 9, -20, 68]
# length = len(lst)
# for i in range(length):
#     maxindex = i
#     for j in range(i+1, length):
#         if lst[maxindex] < lst[j]:
#             maxindex = j
#         if i != maxindex:
#             lst[maxindex],lst[i] = lst[i],lst[maxindex]
# print(lst)

# 8、判断密码强弱

要求密码必须由 10-15位 指定字符组成:

十进制数字

大写字母

小写字母

下划线

要求四种类型的字符都要出现才算合法的强密码

import string
num = input(">>>>>")
num = set(num)
chars = string.ascii_lowercase
upper = string.ascii_uppercase
number = {str(i) for i in range(11)}
ta = {'_'}
length = len(num)

if 15 >= length >= 10:
    chars = set(chars)
    upper = set(upper)
    if num & chars and num & upper and num & number and num & ta and not num - chars - upper - number - ta:
        print('强密码')
    else:
        print("弱密码")
else:
    print("bad password length")

# 9、日志统计

状态200的不同jsp页面访问次数

统计不同类型文件的访问次数,文件类型有静态类(js、css)、图片类(jpg、jpeg、gif、png)、动态类(action、jsp、do)

log_9 = """116.226.208.136 - - [28/Apr/2015:09:01:38 +0800] "GET /js/check.js HTTP/1.1" 304 -
59.53.22.67 - - [28/Apr/2015:09:01:38 +0800] "GET /jquery/jquery.datepick.css HTTP/1.1" 304 -
117.93.56.165 - - [28/Apr/2015:09:01:38 +0800] "GET /jquery/jquery-1.4.2.js HTTP/1.1" 304 -
106.39.189.200 - - [28/Apr/2015:09:01:38 +0800] "GET /jquery/jquery.datepick.js HTTP/1.1" 304 -
111.11.83.162 - - [28/Apr/2015:09:01:38 +0800] "GET /images/shim.gif HTTP/1.1" 304 -
117.93.56.165 - - [28/Apr/2015:09:01:38 +0800] "GET /images/button_ok.gif HTTP/1.1" 304 -
112.80.144.85 - - [28/Apr/2015:09:01:46 +0800] "GET /user/list.jsp HTTP/1.1" 200 7644
117.148.200.56 - - [28/Apr/2015:09:01:46 +0800] "GET /images/i_edit.gif HTTP/1.1" 304 -
175.19.57.147 - - [28/Apr/2015:09:01:46 +0800] "GET /images/button_view.gif HTTP/1.1" 304 -
117.136.63.218 - - [28/Apr/2015:09:05:46 +0800] "GET /user/list.jsp HTTP/1.1" 200 7644
157.55.39.102 - - [28/Apr/2015:09:05:56 +0800] "GET /login.jsp HTTP/1.1" 200 2607
117.93.56.165 - - [28/Apr/2015:09:06:12 +0800] "POST /user_login.action HTTP/1.1" 302 -
223.98.218.205 - - [28/Apr/2015:09:06:12 +0800] "GET /login/home.jsp HTTP/1.1" 200 743
117.136.97.78 - - [28/Apr/2015:09:06:12 +0800] "GET /login/welcome.jsp HTTP/1.1" 200 1142
111.206.221.68 - - [28/Apr/2015:09:06:12 +0800] "GET /login.jsp HTTP/1.1" 200 803
111.206.221.68 - - [28/Apr/2015:09:06:13 +0800] "GET /login.jsp HTTP/1.1" 200 1113"""

log_9 = log_9.splitlines()
count = {}
for log in log_9:
    log = log.split()
    if log[-2] == "200" and log[-4][-4:] == ".jsp":
        count[log[-4]] = count.get(log[-4],0) + 1
print(count)

10、打印矩阵外圈

1 2 6 7

3 5 8 13

4 9 12 14

10 11 15 16

matrix_10 = [[1, 2, 6, 7],
             [3, 5, 8, 13],
             [4, 9, 12, 14],
             [10, 11, 15, 16]]
x = 0
y = 0
length = len(matrix_10)
c = 10
while 1 :
    print(matrix_10[x][y],end=' ')
    if y < length - 1 and x == 0:
        y +=1
    elif y == length - 1 and x < length - 1:
        x +=1
    elif y > 0 and x == length - 1:
        y -= 1
    elif y == 0 and x > 0:
        x -= 1
    if x == 0 and y == 0:
        break

打印矩阵元素 (顺序打印)

1 2 6 7

3 5 8 13

4 9 12 14

10 11 15 16

matrix = [[1, 2, 6, 7],
             [3, 5, 8, 13],
             [4, 9, 12, 14],
             [10, 11, 15, 16]]
target = [0] * 16
target[-1] = 33

item = 0
count = 1
col = 0
flag = True
fin = False
while True:
    if fin:
        break
    item += (1 if flag else 10)
    target[count] = item
    target[-count-1] = 33-item

    step = 9 if flag else -9
    flag = not flag

    col += 1
    count += 1

    for _ in range(col):
        item += step
        target[count] = item
        target[-count -1] = 33-item
        count += 1
        if count == 16//2:
            fin = True
            break
print(target)
for x in target:
    x, y = divmod(x,10)
    print(matrix[x][y],end= ' ')
展开阅读全文

没有更多推荐了,返回首页