- 最近做了点misc的题,解题用的脚本
- 统计密文中大小写和特殊字符出现的次数并排序,密文填到text变量,从小到大排序只需将几处的reverse=True删去
def count_characters(text):
letter_counts = {}
symbol_counts = {}
for char in text:
if char.isalpha():
if char.isupper():
letter_counts[char] = letter_counts.get(char, 0) + 1
else:
symbol_counts[char] = symbol_counts.get(char, 0) + 1
return letter_counts, symbol_counts
def count_lowercase(text):
letter_counts = {}
for char in text:
if char.islower():
letter_counts[char] = letter_counts.get(char, 0) + 1
return letter_counts
def count_characters1(text):
letter_counts = {}
symbol_counts = {}
for char in text:
if char.isalpha():
letter_counts[char] = letter_counts.get(char, 0) + 1
else:
symbol_counts[char] = symbol_counts.get(char, 0) + 1
return letter_counts, symbol_counts
def count_characters2(text):
counts = {}
for char in text:
counts[char] = counts.get(char, 0) + 1
return counts
text = "for letter, count in sorted(letter_counts.items(), key=lambda x: x[1], reverse=True):"
letter_counts, symbol_counts = count_characters(text)
print("大写字母个数:")
for letter, count in sorted(letter_counts.items(), key=lambda x: x[1], reverse=True):
print(f"{letter}: {count}")
print("特殊符号个数:")
for symbol, count in sorted(symbol_counts.items(), key=lambda x: x[1], reverse=True):
print(f"{symbol}: {count}")
letter_counts = count_lowercase(text)
print("小写字母个数:")
for letter, count in letter_counts.items():
print(f"{letter}: {count}")
print("不计大小写字母个数:")
letter_counts, symbol_counts = count_characters1(text)
for letter, count in sorted(letter_counts.items(), key=lambda x: x[1], reverse=True):
print(f"{letter}: {count}")
character_counts = count_characters2(text)
print("总个数:")
for char, count in sorted(character_counts.items(), key=lambda x: x[1], reverse=True):
print(f"{char}: {count}")