目录
7-1 大于身高的平均值
scores = [int(x) for x in input().split()]
avg = sum(scores) / len(scores)
for score in scores:
if score > avg:
print("%d" % score, end=' ')
7-2 输出字母在字符串中位置索引
str1 = input()
a,b = input().split()
for index in range(len(str1)-1,-1,-1):
if str1[index] == a or str1[index] ==b:
print("%d %c" % (index,str1[index]))
7-3 查找指定字符
key = input()
s = input()
for index in range(len(s)-1,-1,-1):
if s[index] == key:
print("index = %d" % index)
break
if index == 0:
print("Not Found")
7-4 字符转换
s = input()
result = 0
for index in s:
if '0' <= index <= '9':
result = result * 10 + int(index)
print(result)
7-5 求整数序列中出现次数最多的数
nums = [int(x) for x in input().split()] # 数据读入
nums.pop(0) # 第一个数据并非需要的数据
appear = {}
key = maxNum = 0
for index in nums:
if index not in appear.keys():
appear[index] = 0
appear[index] += 1
if appear[index] > maxNum:
maxNum = appear[index]
key = index
print("%ld %ld" % (key, maxNum))
7-6 求最大值及其下标
N = int(input())
nums = [int(x) for x in input().split()]
maxNum = nums[0]
flag = 0
for index in range(1, N):
if nums[index] > maxNum:
maxNum = nums[index]
flag = index
print("%d %d" % (maxNum, flag))
7-7 字符串逆序
str1 = input()
for index in range(len(str1) - 1, -1, -1):
print(str1[index], end='')
str1 = input()
for index in range(-1,-len(str1)-1,-1):
print(str1[index], end = '')
7-8 统计大写辅音字母
s = input()
result = 0
for index in s:
if 'B' <= index <= 'Z' and index!='E' and index!='I' and index!='O' and index!='U':
result += 1
print(result)
7-9 字符串排序
# sort 直接排序
string = [index for index in input().split()]
string.sort() # 默认升序
print('After sorted:')
for index in string:
print(index)
# 选择排序
keys = [index for index in input().split()]
for z in range(0, len(keys)-1):
for z1 in range(z+1, len(keys)):
if keys[z] > keys[z1]:
keys[z], keys[z1] = keys[z1], keys[z]
print('After sorted:')
for index in keys:
print(index)
# 自己写排序原理
def cmp(s1, s2):
length = min(len(s1), len(s2))
for index in range(0, length):
if s1[index] < s2[index]:
return True
if s1[index] > s2[index]:
return False
return True if length == len(s1) else False
keys = [index for index in input().split()]
for z in range(0, len(keys) - 1):
for z1 in range(z + 1, len(keys)):
if cmp(keys[z], keys[z1]):
keys[z], keys[z1] = keys[z1], keys[z]
print('After sorted:')
for index in keys:
print(index)
7-10 求整数的位数及各位数字之和
N = int(input())
numSum = num = 0 # 求和,位数
while N > 0:
numSum += N % 10
N //= 10
num += 1
print("%d %d" % (num,numSum))
s = input()
print("%d %d" % (len(s),sum([int(x) for x in s])))
7-11 字符串替换
# ASCII码转换
# chr 数字 -> 字符
# ord 字符 -> 数字(ASCII码)
s = input()
for index in range(0,len(s)):
if 'A' <= s[index] <= 'M':
print(chr(ord('Z') - ord(s[index]) + ord('A')), end='')
elif 'N' <= s[index] <= 'Z':
print(chr(ord('A') - ord(s[index]) + ord('Z')), end='')
else:
print(s[index], end='')
7-12 字符串字母大小写转换
# chr 数字->字符
# ord 字符->数字
s = input()
result = ""
for index in s:
if index=='#': # '#' 不一定在输入的最后一个
break
if 'A' <= index <= 'Z':
result += chr(ord(index)+32) # ASCII: A=65,a=97,差值为32
elif 'a' <= index <= 'z':
result += chr(ord(index)-32)
else:
result += index
print(result)
7-13 统计一行文本的单词个数
s = input().split()
print(len(s))
7-14 删除重复字符
# 利用 ASCII 解法
appear = [0 for index in range(128)]
str1 = input()
for z in str1:
appear[ord(z)] += 1
for index in range(128):
if appear[index] > 0:
print(chr(index), end='')
# 利用字典解法
appear = {}
result = []
s = input()
for index in s:
if index not in appear.keys():
result.append(index)
appear[index] = 0
result.sort()
for index in result:
print(index, end='')
7-15 删除字符
str1 = input()
str2 = input().split()
key1 = str2[0]
key2 = chr(ord(key1)+32) if 'A' <= key1 <= 'Z' else chr(ord(key1)-32)
head = 0
while str1[head] == ' ': # 无需考虑空字符串
head += 1
tail = len(str1) - 1
while str1[tail] == ' ':
tail -= 1
# 遍历
result = ""
for index in range(head, tail + 1):
if str1[index] != key1 and str1[index] != key2:
result += str1[index]
print("result: %s" % result, end='')
7-16 输出10个不重复的英文字母
appear = [1 for x in range(128)] # 利用ASCII判断是否重复出现
str1 = input()
def flag(ch):
if appear[ord(ch)] == 0:
return False
if 'A' <= ch <= 'Z':
appear[ord(ch)] -= 1
appear[ord(ch) + 32] -= 1 # 大小写都只能出现一次
return True
if 'a' <= ch <= 'z':
appear[ord(ch)] -= 1
appear[ord(ch) - 32] -= 1
return True
return False
result = ""
for x in str1:
if flag(x):
result += x
if len(result) < 10:
print('not found')
else:
print(result[:10])
7-17 找最长的字符串
N = int(input())
result = input()
for z in range(1,N):
str1 = input()
if len(str1) > len(result):
result = str1
print("The longest is: %s" % result)
7-18 逆序的三位数
figure = int(input())
result = 0
while figure > 0:
result = result * 10 + figure % 10
figure //= 10
print(result)
7-19 判断回文字符串
s = input()
print(s)
# ok=0表示符合要求
ok = 0
for index in range(0,len(s)//2):
if s[index] != s[len(s)-1-index] :
ok = 1
break
if ok == 0:
print("Yes")
else:
print("No")
7-20 输出大写英文字母
appear = [0 for z in range(200)] # 创建数组,默认0为其对应ASCII字符未出现
result = ""
s = input()
for x in s:
appear[ord(x)] += 1
if 'A' <= x <= 'Z' and appear[ord(x)] == 1:
result += x
if result != "":
print(result)
else:
print("Not Found")
7-21 判断两个字符串是否为变位词
appear = {} # 字符 -> 出现次数
flag = 0 # 判断是否符合要求,默认符合
# 记录每个字符出现次数
s = input()
for index in s:
if index in appear:
appear[index] += 1
else:
appear[index] = 1
# 判断是否出现额外字符,没有则减少一次该字符数量
s = input()
for index in s:
if index not in appear or appear[index] < 0:
flag = 1
break
else:
appear[index] -= 1
# 判断字符的数量是否被减完了没有
for index in appear:
if appear[index] > 0:
flag = 1
break
if flag == 0:
print("yes")
else:
print("no")
7-22 查验身份证
flag = {'1': 0, '0': 1, 'X': 2, '9': 3, '8': 4, '7': 5, '6': 6, '5': 7, '4': 8, '3': 9, '2': 10}
power = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
# 判断 身份证是否符合要求
def trueID(s):
if len(s) != 18:
return 0
result = 0
for z in range(0, 17):
if '0' <= s[z] <= '9':
result += int(s[z]) * power[z]
else:
return 0
if result % 11 != flag[s[17]]:
return 0
return 1
num = nums = int(input()) # num判断是否全正确
for index in range(0, nums):
id = input()
if trueID(id) == 0:
print(id)
else:
num -= 1
if num == 0:
print("All passed")