各位同学,创作不易,能否在文末打赏一瓶饮料呢?(^ _ ^)(文章末尾右下角处)
西北工业大学NOJ-Python程序设计作业题解集合:
NOJ-Python程序设计:第1季:水题(Season 1-Easy) (1-10)
NOJ-Python程序设计:第2季:小段代码(Season 2-Snippet) (11-20)
NOJ-Python程序设计:第3季:循环(Season 3-Loop) (21-30)
NOJ-Python程序设计:第4季:枚举算法(Season 4-Enumeration algorithm) (31-40)
NOJ-Python程序设计:第5季:模块化(Season 5-Modularization) (41-50)
NOJ-Python程序设计:第6季:字符串(Season 6-String) (51-60)
NOJ-Python程序设计:第7季:列表与元组(Season 7-List and Tuple) (61-70)
NOJ-Python程序设计:第8季:集合与字典(Season 8-Sets and Dictionary) (71-80)
NOJ-Python程序设计:第9季:类(Season 9-Class) (81-90)
NOJ-Python程序设计:第10季:挑战算法(Season 10-Challenges) (91-100)
第6季:字符串(Season 6-String)(51-60)
前置知识点
建议大概了解下述函数库的基本运用之后再完成题目会更顺利。
time库
strptime()函数
- 语法
time.strptime(x,"%Y-%m-%d")
(x:时间字符串;后面的是格式化字符串) - 返回struct_time对象。
eval()函数
- 语法
eval(expression)
,比如eval( '3 * x' )
,其中 x = 7 x=7 x=7,那结果就是21
replace()函数
- 语法
str.replace(old, new[, max])
,(返回字符串中的 old(旧字符串) 替换成 new(新字符串)后生成的新字符串,如果指定第三个参数max,则替换不超过 max 次。) - 关于 string 的 replace 方法,需要注意 replace 不会改变原 string 的内容。
- str类型的数据无法直接修改
列表:
- 可以使用append()方法来添加列表项:
a.append('b')
(a是列表) - 删除列表元素:
del a[0]
或者del a[0:2]
(删除[0,2)) - 转换列表元素类型:比如a[]中都是int,需要转换成str,则
a=map(str,a)
- 输出列表的元素:
print(','.join(a))
(中间用’,'分割,并且a中元素的类型是str)。注意它和print(a)的输出的结果是不同的 - sort排序函数:
l.sort(key=len)
(以字符串长度为关键字进行排序)
正则表达式
re 模块使 Python 语言拥有全部的正则表达式功能。
compile 函数根据一个模式字符串和可选的标志参数生成一个正则表达式对象。该对象拥有一系列方法用于正则表达式匹配和替换。
检索和替换
- 语法
re.sub(pattern, repl, string, count=0, flags=0)
- 参数:
- pattern : 正则中的模式字符串。
- repl : 替换的字符串,也可为一个函数。
- string : 要被查找替换的原始字符串。
- count : 模式匹配后替换的最大次数,默认 0 表示替换所有的匹配。
compile 函数
compile 函数用于编译正则表达式,生成一个正则表达式( Pattern )对象,供 match() 和 search() 这两个函数使用。
- 语法:
re.compile(pattern[, flags])
- 参数:
- pattern : 一个字符串形式的正则表达式
合法的日期格式
import time
while(1):
x=input()
if(x==""):
break
else:
try:
if('-'in x):
time.strptime(x,"%Y-%m-%d")
print("True")
elif('/'in x):
time.strptime(x,"%Y/%m/%d")
print("True")
elif('.'in x):
time.strptime(x,"%Y.%m.%d")
print("True")
else:
print("False")
except:
print("False")
# Code By Phoenix_ZH
分词与副词
while(1):
x=input()
if(x==''):
break
if(len(x)<3):
print(x)
continue
if(x[len(x)-3:len(x)]=='ing'):
print(x+'ly')
else:
print(x+'ing')
# Code By Phoenix_ZH
删除字符
while(1):
x=input()
if(x==''):
break
ans=[]
for i in range(len(x)):
if(i%2):
continue
ans.append(x[i])
print(''.join(ans))
# Code By Phoenix_ZH
提取字符串
while(1):
x=input()
if(x==''):
break
if(len(x)<2):
print(x)
else:
print(''.join(x[0]+x[1]+x[len(x)-2]+x[len(x)-1]))
# Code By Phoenix_ZH
公式计算
s=input()
x,y,z=map(int,input().split())
print(s,"=",eval(s),sep='')#eval用于算术运算
# Code By Phoenix_ZH
手机号码
while(1):
x=input()
if(x==''):
break
if(len(x)==11 and x[0:2] in ["13","14","15","17",'18',"19"]):
print("True")
else:
print("False")
# Code By Phoenix_ZH
字符交换
使用str.replace函数
x=input()
x=x.replace(",", "-")
x=x.replace('.', ',')
x=x.replace('-', '.')
print(x)
# Code By Phoenix_ZH
str转换成list修改,在输出str
x=input()
x1,x2=0,0
x=list(x)
for i in range(len(x)):
if(x[i]==','):
x1=i
elif(x[i]=='.'):
x2=i
x[x1]='.'
x[x2]=','
print(''.join(x))
IPv4地址
while(1):
s=input()
if(s==''):
break
x1,x2,x3,x4=map(int,s.split('.'))
if(1<=x1<=255 and 0<=x2<=255 and 0<=x3<=255 and 0<=x4<=255):
print("True")
else:
print("False")
# Code By Phoenix_ZH
同义语
import re
while(1):
s=input()
if(s==''):
break
p=re.compile(r'not *[a-z]* poor')
s=p.sub('good', s)
print(s)
# Code By Phoenix_ZH
单词
l=input().split()
l.sort(key=len)
print(l[0])
print(l[-1])
#Xi'an has a rich and cluturally significant history
# Code By Phoenix_ZH