Set 17
#py101.py
s = input("Please enter a positive integer: ")
cs = 0
for c in s:
cs += int(c)
print('{:=^25}'.format(cs))
#py102.py
a = []
for i in range(8):
a.append([])
for j in range(8):
a[i].append(0)
for i in range(8):
a[i][0] = 1
a[i][i] = 1
for i in range(2,8):
for j in range(1,i):
a[i][j] = a[i-1][j-1] + a[i-1][j]
for i in range(8):
for j in range(i+1):
print("{:3d}".format(a[i][j]),end=" ")
print()
#py103.py
# Please replace the line at ______ with a single line of code or expression
# Note: Do not modify other given code
def proc(strings):
m = 0
lst = []
for i in range(len(strings)):
if len(strings[i]) > m:
m = len(strings[i])
for i in range(len(strings)):
if len(strings[i]) == m:
lst.append(strings[i])
return lst
strings = ['cad' ,'VB', 'Python', 'MATLAB', 'hello', 'world']
result = proc(strings)
print("the longest words are:")
for item in result:
print("{: >25}".format(item))
#py201.py
strings = {'cad', 'PE ', 'Window', 'FM', 'hello', 'world','flowers'}
n = 0
for word in strings:
if word.islower():
n += 1
print(n)
#py202.py
# The following code is a framework for hints
# Please replace the ... with one or more lines of code
# Please replace the line at ______ with a single line of code
## Note: The framework code can be modified as needed to complete the program functionality
def proc(stu_list):
d = {}
for stu in stu_list:
r = stu.split('_')
a,b = r[0],r[1].strip()
if a in d:
d[a] += [b]
else:
d[a] = [b]
lst = sorted(d.items(), key = lambda d:len(d[1]), reverse = True)
return lst
f = open("signup.txt","r")
stu_list = f.readlines()
result = proc(stu_list)
for item in result:
print(item[0], '->', item[1])
f.close()
#py301-1.py
f = open("Around the World in Eighty Days.txt")
fo = open('Around the World in Eighty Days - Chapters.txt','w')
ls = []
for line in f:
temp = line.strip().split()
if temp[0][0] == '第' and '章' in line:
fo.write(line)
# fo.write('\n'.join([i for i in ls]))
f.close()
fo.close()
#py301-2.py
import jieba
f = open('Around the World in Eighty Days.txt')
ls = []
datas = f.readlines()
for i in range(len(datas)):
line = datas[i].split(' ')
if datas[i][0] == '第' and '章' in line[0]:
ls.append(i)
for i in range(len(ls)):
if i != len(ls) - 1:
data = ''.join(datas[ls[i]:ls[i+1]])
else:
data = ''.join(datas[ls[i]:])
s = data.split()[0]
word = jieba.lcut(data)
d={}
for y in word:
if len(y)<2:
continue
d[y] = d.get(y,0) + 1
lis = list(d.items())
lis.sort(key=lambda x:x[1],reverse = True)
print(s,lis[0][0],lis[0][1])
f.close()
Set 18
#py101.py
f = open('poem.txt')
result = []
for line in f.readlines():
line = line.strip()
if len(line) != 0 and line[0] != "#":
result.append(line)
result.sort()
for line in result:
print(line)
f.close()