PTA9文件操作(python3)


我找不着要补的题了……,如果有看到我的哪篇PTA有题目但是答案没有,请告诉我,谢谢哦。

9-1 从文件中查找最长的单词(20分,请勿修改) (20 分)

data.txt中保存有若干个单词,每个单词一行。请编写一个程序从文件中将所有单词读出,找到其中最长的单词,然后将其保存到result.txt中,程序须保存test.py中。注意:题目不要求去重,重复出现的单词算不同的单词。

输入格式:

请查看data.txt
输出格式: 用以下格式输出最长的字符串到result.txt中:

The longest word is: zhang
如果有多个单词,则每个单词用英文逗号间隔(结尾无逗号):

The longest words are: zhang,xiang
程序编写完毕后,请将下载的所有文件按原目录结构打包成zip文件上传

│─main.sh

└─src
data.txt
result.txt
test.py
‘’’
data.txt中保存有n个单词,每个单词一行。
请编写一个程序从文件中将单词读出,找到最长的单词,
然后将其保存到result.txt中。程序须保存test.py中

输出格式:

用以下格式输出最长的字符串到result.txt中:
The longest word is: zhang
如果有多个单词,则每个单词用英文逗号间隔(结尾无逗号):
The longest words are: zhang,xiang

x1 = []
x2 = []
with open('data.txt', 'r') as f:
    fi = f.readlines()
    for i in range(len(fi)):
        fi[i] = fi[i].strip()
        x1.append(len(fi[i]))
    m = max(x1)
    for i in range(len(fi)):
        if len(fi[i]) == m:
            x2.append(fi[i])

with open('result.txt', 'w') as g:
    if len(x2) == 1:
        g.write('The longest word is: {0}'.format(x2[0]))
    else:
        a = ','.join(x2)
        g.write('The longest words are: {0}'.format(a))
g.close()

9-2 求文件行数 (20 分)

下载题目附件,编辑src/目录下的test.py文件,实现读取统计data.txt文件的有效行数, 并将结果输出保存到result.txt文件。(20分) **

**说明: **

(1)有效行指至少包括一个字符行,空行不计为有效行

(2)程序文件名 test.py 不能修改

(3)本地编写测试完成后,将src文件夹打包为 src.zip文件后上传提交

data.txt的内容如下:

python程序设计

人生苦短,我学python
程序设计

抽象过程
自动化求解的计算思维
结合问题思考程序结构
输出结果如下:结果写入到result.txt中

有效行数为:6行

fi = open("data.txt","r",encoding = 'utf-8')
fo = open("result.txt","w",encoding = 'utf-8')

sss = fi.readlines()
ddd = [x.strip() for x in sss if len(x.strip()) > 0]

fo.write("有效行数为:{}行".format(len(ddd)))
fo.close()

9-3 analyze the character distribution of a document (20 分)

download the attachment, edit the test.py file in the src/ directory, read the contents of the sample.txt file, analyze the character distribution of the lowercase letters a-z, count the total number of characters in the file, and output the result in alphabetical order to result.txt file.

notice:
(1) Ignore upper and lower case, i.e. upper case A and lower case a are treated as the same letter.
(2) Only alphanumeric characters are counted, other characters including spaces are not considered.
(3) After finishing your code, compress the src folder into a src.zip file and upload it.

sample.txt

I have a Dream
I have a dream that one day this nation will rise up and live out the true meaning of its creed: "We hold these truths to be self evident that all men are created equal!".
I have a dream that one day on the red hills of Georgia, the sons of former slaves and the sons of former slave owners will be able to sit down together at the table of brotherhood.
I have a dream that one day even the state of Mississippi, a state sweltering with the heat of injustice, sweltering with the heat of oppression, will be transformed into an oasis of freedom and justices.
I have a dream today.

result.txt

There are a total of 467 characters, where each letter is distributed as follows:
a:46, b:6, c:4, d:21, e:69, f:14, g:6, h:29, i:32, j:2, l:20, m:12, n:25, o:36, p:5, q:1, r:27, s:32, t:49, u:7, v:10, w:10, y:4

fi = open("sample.txt","r")
fo = open("result.txt","w")
txt = fi.read()
num = 0
d = {}
for ele in txt:
    x = ele.split()
    for i in x:
        if i.isalpha():
            num += 1
            i = i.lower()
            d[i] = d.get(i, 0) + 1
values = d.values()
keys = d.keys()
d = sorted(d.items(), key=lambda x: x[0], reverse=False)
long = 'There are a total of {} characters, where each letter is distributed as follows:\n'.format(num)
fom = ''

keys = []
values = []
for i in d:
    keys.append(i[0])
    values.append(i[1])


new_d = dict(zip(keys,values))

x = len(new_d)
n = 0
for i in new_d.keys():
    n = n + 1
    if n != x:
        fom += i+":"+str(new_d[i]) + ", "
    else:
        fom += i+":"+str(new_d[i])

fo.write(long+fom)
     

在这里插入图片描述

文件src和main.sh打包成压缩包交上,我的代码是test.py

  • 12
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值