Python习题

您所在的位置:网站首页 例题英文简写 Python习题

Python习题

2024-03-23 21:05| 来源: 网络整理| 查看: 265

1、(字符串)数字月份转字符串(OJ 3514) 编写程序,实现月份数字向英文缩写的转换。从键盘上输入一个表示月份的数字(1 ~ 12),输出对应月份的英文缩写,不同月份对应的缩写: 1月为Jan,2月为Feb,3月为Mar,4月为Apr,5月为May,6月为Jun,7月为Jul,8月为Aug,9月为Sep,10月为Oct,11月为Nov,12月为Dec。

moths="JanFebMarAprMayJunJulAugSepOctNovDec" n=input() pos=(int(n)-1)*3 mothAbbrev=moths[pos:pos+3] print(mothAbbrev)

2、(正则表达式)习题8.1(OJ3515)检查重复单词 有一段英文文本,其中有单词连续重复了2次,编写程序检查重复的单词并只保留一个。例如文本内容为“This is is a desk.”,程序输出为“This is a desk.”。

import re x = input() pattern = re.compile(r'\b(\w+)(\s+\1){1,}\b') matchResult = pattern.search(x) x = pattern.sub(matchResult.group(1),x) print(x)

3、(正则表达式)习题7.1(OJ3513)字母纠正 假设有一段英文,其中有单独的字母“I”误写为“i”,请编写程序进行纠正。要求,必须使用正则表达式。

x = input() import re pattern = re.compile(r'(?:[^\w]|\b)i(?:[^\w])') while True: result = pattern.search(x) if result: if result.start(0) != 0: x = x[:result.start(0)+1]+'I'+x[result.end(0)-1:] else: x = x[:result.start(0)]+'I'+x[result.end(0)-1:] else: break print(x)


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3