1.摩尔斯电码转换
# before = 'abcdefghijklmnopqrstuvwxyz'
# after = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]
# table = dict(zip(before, after))
# 前三行执行结果是第6行字典,可手动生成字典
morse_dic = {'a': '.-', 'b': '-...', 'c': '-.-.', 'd': '-..', 'e': '.', 'f': '..-.', 'g': '--.', 'h': '....', 'i': '..', 'j': '.---', 'k': '-.-', 'l': '.-..', 'm': '--', 'n': '-.', 'o': '---', 'p': '.--.', 'q': '--.-', 'r': '.-.', 's': '...', 't': '-', 'u': '..-', 'v': '...-', 'w': '.--', 'x': '-..-', 'y': '-.--', 'z': '--..'}
txt = input().lower()
for c in txt:
print(morse_dic.get(c, c), end='')
2.凯撒密码——加密
def caesar_cipher(text):
"""接收一个字符串为参数,采用字母表和数字中后面第3个字符代替当前字符的方法
对字符串中的字母和数字进行替换,实现加密效果,返回值为加密的字符串。
例如:2019 abc 替换为5342 def """
# before = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
# after = 'defghijklmnopqrstuvwxyzabcDEFGHIJKLMNOPQRSTUVWXYZABC3456789012'
# table = dict(zip(before,after))
# 前三行执行结果是第6行字典,可手动生成字典
table = {'a': 'd', 'b': 'e', 'c': 'f', 'd': 'g', 'e': 'h', 'f': 'i', 'g': 'j', 'h': 'k', 'i': 'l', 'j': 'm', 'k': 'n', 'l': 'o', 'm': 'p', 'n': 'q', 'o': 'r', 'p': 's', 'q': 't', 'r': 'u', 's': 'v', 't': 'w', 'u': 'x', 'v': 'y', 'w': 'z', 'x': 'a', 'y': 'b', 'z': 'c', 'A': 'D', 'B': 'E', 'C': 'F', 'D': 'G', 'E': 'H', 'F': 'I', 'G': 'J', 'H': 'K', 'I': 'L', 'J': 'M', 'K': 'N', 'L': 'O', 'M': 'P', 'N': 'Q', 'O': 'R', 'P': 'S', 'Q': 'T', 'R': 'U', 'S': 'V', 'T': 'W', 'U': 'X', 'V': 'Y', 'W': 'Z', 'X': 'A', 'Y': 'B', 'Z': 'C', '0': '3', '1': '4', '2': '5', '3': '6', '4': '7', '5': '8', '6': '9', '7': '0', '8': '1', '9': '2'}
ciphertext = ''
for c in text:
ciphertext = ciphertext + table.get(c, c)
return ciphertext
if __name__ == '__main__':
plaintext = input()
print(caesar_cipher(plaintext))
3.缩写月份单词
month = input().lower().capitalize() # 输入转小写再将首字母大写
month_dic = {'January':'Jan.', 'February':'Feb.', 'March':'Mar.', 'April':'Apr.', 'May':'May.', 'June':'Jun.', 'July':'Jul.', 'August':'Aug.', 'September':'Sept.', 'October':'Oct.',
'November':'Nov.', 'December':'Dec.'}
print(month_dic.get(month,'spelling mistake'))
4.本月天数
def is_leap(year):
return year % 4 == 0 and year % 100 != 0 or year % 400 == 0
def days_of_month(date_str):
"""根据输入的年月日,返回该月的天数"""
day_dic = {'01': 31, '02': 28, '03': 31, '04': 31, '05': 31, '06': 31, '07': 31, '08': 31, '09': 31, '10': 31,
'11': 31, '12': 31, }
day = day_dic.get(date_str[4:6])
if is_leap(int(date_str[:4])):
day = day + 1
return day
if __name__ == '__main__':
date_in = input() # 输入一个年月日
print(days_of_month(date_in))
5.字符串加密
# before = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
# after = 'defghijklmnopqrstuvwxyzabcFGHIJKLMNOPQRSTUVWXYZABCDE'
# table = dict(zip(before,after))
# 前三行执行结果是第6行字典,可手动生成字典
table = {'a': 'd', 'b': 'e', 'c': 'f', 'd': 'g', 'e': 'h', 'f': 'i', 'g': 'j', 'h': 'k', 'i': 'l', 'j': 'm', 'k': 'n', 'l': 'o', 'm': 'p', 'n': 'q', 'o': 'r', 'p': 's', 'q': 't', 'r': 'u', 's': 'v', 't': 'w', 'u': 'x', 'v': 'y', 'w': 'z', 'x': 'a', 'y': 'b', 'z': 'c', 'A': 'F', 'B': 'G', 'C': 'H', 'D': 'I', 'E': 'J', 'F': 'K', 'G': 'L', 'H': 'M', 'I': 'N', 'J': 'O', 'K': 'P', 'L': 'Q', 'M': 'R', 'N': 'S', 'O': 'T', 'P': 'U', 'Q': 'V', 'R': 'W', 'S': 'X', 'T': 'Y', 'U': 'Z', 'V': 'A', 'W': 'B', 'X': 'C', 'Y': 'D', 'Z': 'E'}
text = input()
for c in text:
print(table.get(c, c), end='')
6.绩点计算
score = {'A': 4.0, 'A-': 3.7, 'B+': 3.3, 'B': 3.0, 'B-': 2.7, 'C+': 2.3, 'C': 2.0, 'C-': 1.5, 'D': 1.3, 'D-': 1.0, 'F': 0.0}
credit_ls, gpa_ls = [],[]
while True:
s = input()
if s == '-1':
break
elif s in score.keys():
credit = float(input())
credit_ls.append(credit)
gpa_ls.append(score.get(s) * credit)
else:
print('data error')
gpa_ave = sum(gpa_ls) / sum(credit_ls)
print(f'{gpa_ave:.2f}')
7.字典增加元素
dict1 = {'赵小明': '13299887777', '特明朗': '814666888', '普希京': '522888666', '吴小京': '13999887777'}#查询键是否存在,存在时返回键值对,不存在返回"数据不存在"
new_name = input()
new_value = input()
if new_name in dict1:
print("您输入的姓名在通讯录中已存在" )
else:
dict1[new_name] = new_value
for key in dict1:
print(f'{key}:{dict1.get(key)}')
8.查询省会
capitals = {'湖南': '长沙', '湖北': '武汉', '广东': '广州', '广西': '南宁', '河北': '石家庄', '河南': '郑州', '山东': '济南', '山西': '太原', '江苏': '南京', '浙江': '杭州', '江西': '南昌', '黑龙江': '哈尔滨', '新疆': '乌鲁木齐', '云南': '昆明', '贵州': '贵阳', '福建': '福州', '吉林': '长春','安徽': '合肥', '四川': '成都', '西藏': '拉萨', '宁夏': '银川', '辽宁': '沈阳', '青海': '西宁', '甘肃': '兰州', '陕西': '西安','内蒙古': '呼和浩特', '台湾': '台北', '北京': '北京', '上海': '上海', '天津': '天津', '重庆': '重庆', '香港': '香港', '澳门': '澳门'}
while province := input():
print(capitals.get(province, '输入错误'))
#00320030003400301655908273074
9.用字典来统计词频
freq_dict = eval(input())
for word in input().split():
freq_dict[word] = freq_dict.get(word, 0)+1
print(freq_dict)
10.查询高校信息
with open('university.csv','r',encoding='utf-8') as f:
ls = [x.strip().split(',') for x in f]
univ_dic = {x[1]: ','.join(x) for x in ls}
univ_name = input()
print(univ_dic.get('学校名称'))
print(univ_dic.get(univ_name))
11.查询高校名(未用到,但与前一题相似)
with open('university.csv','r',encoding='utf-8') as f:
ls = [x.strip().split(',') for x in f]
key_word = input()
for x in ls:
if key_word in x[1]:
print(x[1])
12.罗马数字转换
d = {'I': 1, 'IV': 3, 'V': 5, 'IX': 8, 'X': 10, 'XL': 30, 'L': 50, 'XC': 80, 'C': 100, 'CD': 300, 'D': 500, 'CM': 800,'M': 1000}
# enumerate('MCCIII') # 给每个元素加一个序号[(0, 'M'), (1, 'C'), (2, 'C'), (3, 'I'), (4, 'I'), (5, 'I')]
s = input()
print(sum(d.get(s[max(i - 1, 0):i + 1], d[n]) for i, n in enumerate(s)))
13.大小写转换
# before = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
# after = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
# table = dict(zip(before,after))
# 前三行执行结果是第6行字典,可手动生成字典
table = {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D', 'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H', 'i': 'I', 'j': 'J', 'k': 'K', 'l': 'L', 'm': 'M', 'n': 'N', 'o': 'O', 'p': 'P', 'q': 'Q', 'r': 'R', 's': 'S', 't': 'T', 'u': 'U', 'v': 'V', 'w': 'W', 'x': 'X', 'y': 'Y', 'z': 'Z', 'A': 'a', 'B': 'b', 'C': 'c', 'D': 'd', 'E': 'e', 'F': 'f', 'G': 'g', 'H': 'h', 'I': 'i', 'J': 'j', 'K': 'k', 'L': 'l', 'M': 'm', 'N': 'n', 'O': 'o', 'P': 'p', 'Q': 'q', 'R': 'r', 'S': 's', 'T': 't', 'U': 'u', 'V': 'v', 'W': 'w', 'X': 'x', 'Y': 'y', 'Z': 'z'}
for c in input():
print(table.get(c, c),end='')