1.摩尔斯电码转换

  1. # before = 'abcdefghijklmnopqrstuvwxyz'
  2. # after = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]
  3. # table = dict(zip(before, after))
  4. # 前三行执行结果是第6行字典,可手动生成字典
  5. 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': '--..'}
  6. txt = input().lower()
  7. for c in txt:
  8. print(morse_dic.get(c, c), end='')

2.凯撒密码——加密

  1. def caesar_cipher(text):
  2. """接收一个字符串为参数,采用字母表和数字中后面第3个字符代替当前字符的方法
  3. 对字符串中的字母和数字进行替换,实现加密效果,返回值为加密的字符串。
  4. 例如:2019 abc 替换为5342 def """
  5. # before = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
  6. # after = 'defghijklmnopqrstuvwxyzabcDEFGHIJKLMNOPQRSTUVWXYZABC3456789012'
  7. # table = dict(zip(before,after))
  8. # 前三行执行结果是第6行字典,可手动生成字典
  9. 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'}
  10. ciphertext = ''
  11. for c in text:
  12. ciphertext = ciphertext + table.get(c, c)
  13. return ciphertext
  14. if __name__ == '__main__':
  15. plaintext = input()
  16. print(caesar_cipher(plaintext))

3.缩写月份单词

  1. month = input().lower().capitalize() # 输入转小写再将首字母大写
  2. month_dic = {'January':'Jan.', 'February':'Feb.', 'March':'Mar.', 'April':'Apr.', 'May':'May.', 'June':'Jun.', 'July':'Jul.', 'August':'Aug.', 'September':'Sept.', 'October':'Oct.',
  3. 'November':'Nov.', 'December':'Dec.'}
  4. print(month_dic.get(month,'spelling mistake'))

4.本月天数

  1. def is_leap(year):
  2. return year % 4 == 0 and year % 100 != 0 or year % 400 == 0
  3. def days_of_month(date_str):
  4. """根据输入的年月日,返回该月的天数"""
  5. day_dic = {'01': 31, '02': 28, '03': 31, '04': 31, '05': 31, '06': 31, '07': 31, '08': 31, '09': 31, '10': 31,
  6. '11': 31, '12': 31, }
  7. day = day_dic.get(date_str[4:6])
  8. if is_leap(int(date_str[:4])):
  9. day = day + 1
  10. return day
  11. if __name__ == '__main__':
  12. date_in = input() # 输入一个年月日
  13. print(days_of_month(date_in))

5.字符串加密

  1. # before = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  2. # after = 'defghijklmnopqrstuvwxyzabcFGHIJKLMNOPQRSTUVWXYZABCDE'
  3. # table = dict(zip(before,after))
  4. # 前三行执行结果是第6行字典,可手动生成字典
  5. 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'}
  6. text = input()
  7. for c in text:
  8. print(table.get(c, c), end='')

6.绩点计算

  1. 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}
  2. credit_ls, gpa_ls = [],[]
  3. while True:
  4. s = input()
  5. if s == '-1':
  6. break
  7. elif s in score.keys():
  8. credit = float(input())
  9. credit_ls.append(credit)
  10. gpa_ls.append(score.get(s) * credit)
  11. else:
  12. print('data error')
  13. gpa_ave = sum(gpa_ls) / sum(credit_ls)
  14. print(f'{gpa_ave:.2f}')

7.字典增加元素

  1. dict1 = {'赵小明': '13299887777', '特明朗': '814666888', '普希京': '522888666', '吴小京': '13999887777'}#查询键是否存在,存在时返回键值对,不存在返回"数据不存在"
  2. new_name = input()
  3. new_value = input()
  4. if new_name in dict1:
  5. print("您输入的姓名在通讯录中已存在" )
  6. else:
  7. dict1[new_name] = new_value
  8. for key in dict1:
  9. print(f'{key}:{dict1.get(key)}')

8.查询省会

  1. capitals = {'湖南': '长沙', '湖北': '武汉', '广东': '广州', '广西': '南宁', '河北': '石家庄', '河南': '郑州', '山东': '济南', '山西': '太原', '江苏': '南京', '浙江': '杭州', '江西': '南昌', '黑龙江': '哈尔滨', '新疆': '乌鲁木齐', '云南': '昆明', '贵州': '贵阳', '福建': '福州', '吉林': '长春','安徽': '合肥', '四川': '成都', '西藏': '拉萨', '宁夏': '银川', '辽宁': '沈阳', '青海': '西宁', '甘肃': '兰州', '陕西': '西安','内蒙古': '呼和浩特', '台湾': '台北', '北京': '北京', '上海': '上海', '天津': '天津', '重庆': '重庆', '香港': '香港', '澳门': '澳门'}
  2. while province := input():
  3. 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='')