考试整理
- 第一题 :从集合 numSet = {1,9,15,60,77,21,18,6,-5,-21} 中随机取数,要求取到的数字可以被 3 整除,但是不能被5整除,一共取 8 个这样的数字。1.请分别打印出这 8 个数字; 2.请打印出这 8 个数字的和。
import random
def num_set(n_set):
sum_n=0
count=0
while count!=8:
num=random.sample(n_set,1)
if num[0]%3==0 and num[0]%5!=0:
print('第{}个数是{}'.format(count+1,num[0]))
sum_n+=num[0]
count+=1
return '8个数的集合是{}'.format(sum_n)
numSet = {1,9,15,60,77,21,18,6,-5,-21}
print(num_set(numSet))
-
第二题
#从下面链接中找出 https 开头的链接并且该链接以 .gz 结尾,请将将这些符合要求链接的文件名按顺序输出。‘’‘http://mirrors.kernel.org/gnu/coreutils/coreutils-6.11.tar.gz
http://mirrors.kernel.org/gnu/bash/bash-5.0.tar.gz
https://www.baidu.com/
https://www.apache.org/dyn/closer.cgi/hadoop/common/hadoop-3.1.2/hadoop-3.1.2-src.tar.gz
https://apache.org/dist/hbase/2.1.4/api_compare_2.1.3_to_2.1.4RC1.html
https://www.apache.org/dyn/closer.lua/hbase/2.1.4/hbase-2.1.4-src.tar.gz
https://tomcat.apache.org/download-90.cgi
http://mirrors.kernel.org/gnu/grub/grub-2.02.tar.xz
http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.19/bin/apache-tomcat-9.0.19-fulldocs.tar.gz
https://archive.apache.org/dist/zookeeper/current/zookeeper-3.4.14.tar.gz
https://hub.docker.com/editions/community/docker-ce-desktop-mac
https://home.firefoxchina.cn/
http://docs.ceph.com/docs/master/’’’import re def link(): link_index='''http://mirrors.kernel.org/gnu/coreutils/coreutils-6.11.tar.gz http://mirrors.kernel.org/gnu/bash/bash-5.0.tar.gz https://www.baidu.com/ https://www.apache.org/dyn/closer.cgi/hadoop/common/hadoop-3.1.2/hadoop-3.1.2-src.tar.gz https://apache.org/dist/hbase/2.1.4/api_compare_2.1.3_to_2.1.4RC1.html https://www.apache.org/dyn/closer.lua/hbase/2.1.4/hbase-2.1.4-src.tar.gz https://tomcat.apache.org/download-90.cgi http://mirrors.kernel.org/gnu/grub/grub-2.02.tar.xz http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.19/bin/apache-tomcat-9.0.19-fulldocs.tar.gz https://archive.apache.org/dist/zookeeper/current/zookeeper-3.4.14.tar.gz https://hub.docker.com/editions/community/docker-ce-desktop-mac https://home.firefoxchina.cn/ http://docs.ceph.com/docs/master/''' line = re.findall('https.*?.gz',link_index) return line print(link())
-
第三题
#随机生成 20 个 1到100 的整数,然后请分别使用冒泡排序和选择排序两种算法对这些数字进行排序。
import random
nums=[random.randint(1,100)for i in range(20)]
print(nums)
def buffble_sort():
for i in range(len(nums)):
flag =True
for j in range(len(nums)-1):
if nums[j]>nums[j+1]:
nums[j],nums[j+1]=nums[j+1],nums[j]
flag =False
if flag:
return nums
return nums
def selection_sort():
for i in range(len(nums)):
min=i
for j in range(i+1,len(nums)):
if nums[min]>nums[j]:
min = j
if min !=i:
nums[min],nums[i]=nums[i],nums[min]
return nums
print(buffble_sort())
print(selection_sort())
- 第四题
请生成6个ID字符串,格式为 随机3个小写字母 . 随机4个大写字母 . 随机5个数字字符
import random
print(['{}.{}.{}'.format(''.join([chr(random.randint(97,122)) for i in range(3)]),''.join([chr(random.randint(65,90)) for i in range(4)]),''.join([str(random.randint(0,9))for i in range(5)]))for i in range(6)])
- 第五题
输入一个整数,判断奇偶 输出成十字架格式
def nums(num):
num1=num//2+1
num2 = num//2
per=num//2+1
for i in range(1,num+1):
for j in range(1,num+1):
if num%2==0:
if i==num1-1 or i==num1:
print('{:>2}'.format(num),end='')
elif j==num1 :
print('{}{:>{}}{:>2}'.format(' '*num2,num,per-1,num),end='')
else:
if i==num1:
print('{:>2}'.format(num),end='')
elif j==num1 :
print('{}{:>{}}'.format(' '*num2,num,per+1),end='')
print()
print(nums(2))