Python入门测试

1.比如自然数10以下能被3或者5整除的有,3,5,6和9,那么这些数字的和为23.

求能被3或者5整除的1000以内数字的和

 1 multiple_of_threes=[]
 2 for multiple_of_three in range(0,1001,3):
 3     multiple_of_threes.append(multiple_of_three)
 4 sum_of_threes=sum(multiple_of_threes)
 5 print(multiple_of_threes)
 6 print(sum_of_threes)
 7 multiple_of_fives=[]
 8 for multiple_of_five in range(0,1001,5):
 9     multiple_of_fives.append(multiple_of_five)
10 sum_of_fives=sum(multiple_of_fives)
11 print(multiple_of_fives)
12 print(sum_of_fives)
13 print(sum_of_threes+sum_of_fives)
14 multiple_of_threes.extend(multiple_of_fives)  #3的倍数和5的倍数合并成一个列表
15 print(sum(multiple_of_threes))

 2.在一个数组指定数组里面移除指定的数字,并返回新的数组并从大到小排序

比如:

nums=[1,6,6,3,6,2,10,2,100],remove_num=6

nums=[11,22,6,6,58,36]
while 6 in nums:
    nums.remove(6)
nums.sort(reverse=True)
print(nums)

3.从排序好的任意数组列表里面删除重复元素(你不知道列表里面有多少个重逢的元素)

比如:

nums=[1,3,3,5,5,8,10,10,100,100],处理完之后是:[1, 3, 5, 8, 10, 100]

nums=[1,3,3,5,5,8,10,10,100,100]

 

#删除列表中的重复值
num=[11,22,6,13,6,11,58,36]
num2=[]
for i in num:
     if i not in num2:
         num2.append(i)
print(num2)#[11, 22, 6, 13, 58, 36]
#转换成集合,再转换成列表输出
num=[11,22,6,13,6,11,58,36]
print(list(set(num)))#[36, 6, 11, 13, 22, 58]

4.从排好序的数组里面,删除重复的元素.重复的数字最多只能出现2次

nums=[1,1,1,2,2,3] 

要求返回nums=[1,1,2,2,3] 

1 #删除列表中重复3次及以上的数值
2 list=[1,1,1,2,2,2,3,3,4,4]
3 for i in list:
4     if list.count(i)>2:
5         list.remove(i)
6 print(list)
1 #将一个列表中重复3次以下的数值插入到另一个列表中
2 list=[1,1,1,2,2,2,3,3,4,4]
3 list2=[]
4 for i in list:
5     if list.count(i)<3:
6         list2.append(i)
7 print(list2)

5.给定2个字符串s1,s2,判定s2能否给s1做循环移位得到字符串的包含。比如:

s1="AABBCD",s2="CDAA".

 

6.给定一个字符串,寻找没有字符串重复的最长子字符串.

比如:"abcabcbb" 找到的是"abc",长度为3,比如"bbbbb"找到的是"b",长度为1

 

 

7.有一串长的字符串 names="LI XIA  ,ZHAO MING  ,LAO WANG *,DA XIONG >,LI MEI MEI,"

      "CHANG JIANG,LI QIANG,ZHANG WU JI,ZHANG SAN FENG,"

      "DU GU QIU BAI,QIAO FENG"

要求:

1).过滤出所有的名字,去掉每个名字的左右的空格和乱码,每个名字的首字母大小,比如'LAO WANG *',处理成'Lao wang'

2).统计出所有名字里面名字最长的

3).统计出同姓的人名单

names="LI XIA  ,ZHAO MING  ,LAO WANG *,DA XIONG >,LI MEI MEI," 
    "CHANG JIANG,LI QIANG,ZHANG WU JI,ZHANG SAN FENG,DU GU QIU BAI,QIAO FENG"
import re
#去掉字符串里的特殊字符和空格,并让名字首字母大写
names = re.sub('[*>]','',names)
names = names.lower()
names = names.split(',')
names = [name.strip().capitalize() for name in names]
names2 = ','.join(names)
print(names2)
#统计列表中名字最长的
num_list = [len(name) for name in names]
long_name_index = num_list.index(max(num_list))
long_name = names[long_name_index]
print(long_name)
#统计出同姓人名单
print(names)
#把列表中的姓名分开,组成一个列表,所有姓名列表加入一个大列表
names3 = [name.split(' ') for name in names]
print(names3)
#把所有的姓存入一个列表
first_name = [name_list[0] for name_list in names3]
print(first_name)
#找出同姓的人
same_first_name = []
for i in first_name:
    if first_name.count(i) > 1:
        name_index = first_name.index(i)
        same_first_name.append(names[name_index])
print(same_first_name)

8.数字1到5可以被写成:one,two,three,four,five,因此这些字母的总长度为:

3+3+5+4+4=19,现在求序列1到1000(包括1000),这些数字写成单词,总长度为多少

  • 比如 342(three hundred and forty-two)为23字母,空格和-不计算.

  • 比如 115(one hundred and fifteen)为20个字母

  • 比如 1000(one hundred)为11个字母

原文地址:https://www.cnblogs.com/charliedaifu/p/9865192.html