gpt4 book ai didi

python - Python 中根据字符长度解析/打印; CodeEval 上只有 98%

转载 作者:行者123 更新时间:2023-11-30 23:09:18 24 4
gpt4 key购买 nike

所以我现在正在解决 CodeEval 问题,并且由于某种原因,我无法超过 98/100 分数。

以下是 CodeEval 挑战赛的链接:

https://www.codeeval.com/open_challenges/167/

这是我的代码:

# -*- coding: utf-8 -*-
import sys

zeefile = open(sys.argv[1])

for line in zeefile:
if len(line) <= 55:
sys.stdout.write(line)
elif len(line) > 55:
line = line[:40].rsplit(" ", 1)[0]
sys.stdout.write(line)
sys.stdout.write('... <Read More> \n')

即使有一些开发人员比我更有才华,我也已经在这堵墙上撞了几个小时了。

至少可以说,我们很困惑。最终,这不是什么大问题,但我想知道这里是否遗漏了一些东西,以便我可以从中学习。

我一遍又一遍地检查了代码,检查了输入,检查了输出...我找不到任何不一致之处,或者任何表明我遗漏了最后 2% 的内容一个成功的解决方案。

知道我们缺少什么来解释为什么这不能作为问题的 100% 合法解决方案回来吗?我希望一些新鲜的眼睛和敏锐的头脑可以帮助我解决这个问题!非常感谢!

最佳答案

尝试以下代码(代码评估 100%):

import sys

with open(sys.argv[1], 'r') as in_f:
for line in in_f:
line = line.strip()
if len(line) > 55:
line = "{0}... <Read More>".format(line[:40].rsplit(" ", 1)[0].rstrip())
sys.stdout.write("{0}\n".format(line))

我使用了这个文件:

Tom exhibited.
Amy Lawrence was proud and glad, and she tried to make Tom see it in her face - but he wouldn't look.
Tom was tugging at a button-hole and looking sheepish.
Two thousand verses is a great many - very, very great many.
Tom's mouth watered for the apple, but he stuck to his work.

并得到以下输出:

Tom exhibited.
Amy Lawrence was proud and glad, and... <Read More>
Tom was tugging at a button-hole and looking sheepish.
Two thousand verses is a great many -... <Read More>
Tom's mouth watered for the apple, but... <Read More>

Code Eval Score

关于python - Python 中根据字符长度解析/打印; CodeEval 上只有 98%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31305390/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com