121.函数main()接收一个字符串s,要求返回字符串s中长度为4的单词组成的列表。例如,当s为'If the implementation is easy to explain, it may be a good idea.'时返回['easy,good''idea']。删除下面代码中的pass语句,替换为自己的代码,完成要求的功能。其中导入的模块re不是必须使用的,可以根据情况自行决定是否使用。
import re
def main(s):
return re.findall(r'\b\w{4}\b',s)
122.函数main()接收任意多个列表、元组或字符串,要求返回它们中下标1的元素组成的列表。例如·main([1,2,3],'456',(7,8,9))的结果为[2,'5',8]。删除下面代码中的pass语句,替换为自己的代码,完成要求的功能。已导入的对象不是必须使用的,是否使用可以自己决定。
from operator import itemgetter
def main(*iterables):
getIndex = itemgetter(1)
ret = []
for i in iterables:
ret.append(getIndex(i))
return ret
123.函数main()接收一个任意字符串text,要求返回其中每个字符的Unicode编码减1之后对应的字符连接成的新字符串。例如,main('董付国')返回'蓋仗固', 因为'董付国'中三个字符的Unicode编码分别为33891、20184、22269,分别减1后得到33890、20183、22268,转换为对应的字符再连接得到'蒸仗图'。删除下面代码中的pass语句,替换为自己的代码,完成要求的功能。
def func(s):
return chr(ord(s)-1)
def main(text):
return ''.join((map(func,text)))
124.函数main()接收一个表示图像文件路径的参数fn,对应的图像文件可能是'RGB'模式的彩色图像,也可能是’L'模式的灰度图像,文件格式可能为PNG、JPG、BMP之一,逐个读取图像中像素的颜色值,如果是彩色图像就计算该像素颜色值各分量的平均值。然后计算图像中灰度值或颜色分量平均值大于等于200的像素数量与该图像中像素总数量的商,对结果进行四舍五入后保留最多2位小数,返回最终计算结果。删除下面代码中的pass语句,替换为自己的代码,完成要求的功能。代码中已导入的对象不是必须使用的,是否使用可以自行决定。服务器已安装扩展库pillow7.0.0。
from itertools import product
from PIL import Image
def main(fn):
with Image.open(fn) as img:
mode = img.mode
width, height = img.size
total_pixels = width * height
count = 0
for x, y in product(range(width), range(height)):
pixel = img.getpixel((x, y))
if mode == 'RGB':
brightness = sum(pixel) / 3
else:
brightness = pixel
if brightness >= 200:
count += 1
ratio = count / total_pixels
return round(ratio,2)
125.函数main()接收一个包含若干整数的列表data·将其转换为numpy数组,然后返回其中所有小于30或大于70的数字之和。例如,data为[79,22,84,8,11,51,54,17,92,47]时返回313。删除下面代码中的pass语句,替换为自己的代码,完成要求的功能。
from numpy import array
def main(data):
data = array(data)
return sum(data[(data < 30) | (data > 70)])
126.函数main()接收两个可迭代对象iterable1和iterable2,要求把它们对应位置上的元素组合到一起变为元组,返回包含这些元组的列表。如果iterable1和iterable2的长度不一样,最终结果以长的为准,把短的一个可选代对象使用数字a补齐到和元素多的那个可迭代对象一样长。例如,main('123',[5,6,7,8])返回[('1',5),('2',6),('3',),(0,8)]。删除下面代码中的pass语句,替换为自己的代码,完成要求的功能。代码中已导入的标准库不是必须使用的,是否使用可以自己决定。
import itertools
def main(iterable1, iterable2):
return list(itertools.zip_longest(iterable1, iterable2, fillvalue=0))
127.函数main()接收两个可选代对象iterable1和iterable2,要求返回这两个可迭代对象的笛卡尔积组成的列表。例如,main([1,2],'abcd')返回[(1,'a'),(1,'b'),(1,c')(1,'d')(2,'a'),(2,'b')(2,'c') (2,'d')删除下面的pass语句,替换为自己的代码,完成要求的功能。已导入的标准库不是必须使用的,是否使用可以自己决定。
import itertools
def main(iterable1, iterable2):
return list(itertools.product(iterable1, iterable2))
128.函数main()接收两个包含若干数字目长度相等的可选代对象iterable1和iterable2,如果这两个可选代对象所有对应位置上的数字的绝对误差都小于等于1就返回True,否则返回False。例如'main([3,5,6,7.9],[5,6,7,8.1])返回False'main([1,2],[1.4,2.9])返回True。删除下面的pass语句,替换为自己的代码,完成要求的功能。已导入的标准库对象不是必须使用的,是否使用可以自己决定。
from math import isclose
from functools import partial
isclose = partial(isclose, abs_tol=1)
def main(iterable1, iterable2):
return all(isclose(a, b) for a, b in zip(iterable1, iterable2))
129.函数main()接收一个包含若干数字的可选代对象data,要求检查其中的元素是否严格升序,也就是所有相邻元素都是前面小于后面的。例如,main([1,3,1.4,2.9])返回FaIse,main(range(5))返回True。删除下面的pass语句,替换为自己的代码,完成要求的功能。已导入的标准库对象不是必须使用的,是否使用可以自己决定。
from operator import lt
def main(data):
items = list(data)
adjacent_pairs = zip(items, items[1:])
return all(lt(prev, curr) for prev, curr in adjacent_pairs)
130.函数main()接收两个包含若干数字且长度相等的列表values和weights·要求计算并返回加权平均值,以列表weights中的数字为权重,结果保留最多3位小数。例如main([1,2,3,4],[1,2,3,4])的计算过程为(1*1+2*2+3*3+4*4)/(1+2+3+4)= 3.0,返回3.0。main([1,2,3,4],[4,3,2,1])的计算过程为(1*4 + 2*3 + 3*2 + 4*1)/(4+3+2+1)=2.0,返回2.0。删除下面的pass语句,替换为自己的代码,完成要求的功能。已导入的标准库对象不是必须使用的,是否使用可以自己决定。
from operator import mul
def main(values, weights):
numerator = sum(map(mul, values, weights))
denominator = sum(weights)
return numerator/denominator