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")
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= ' ')