所以我现在正在解决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%合法解决问题的方式回归?我希望一些新鲜的眼睛和敏锐的头脑可以帮助我解决这个问题!非常感谢你!
答案 0 :(得分:3)
尝试以下代码(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>