Spotify 技术难题 - Python 中的 stdin
我正在尝试解决 Spotify 技术难题上的双边问题。 http://www.spotify.com/us/jobs/tech/bilingual- items/ 我的计算机上有一些东西正在运行,它从文件 input.txt 读取输入,并将其输出到 ouput.txt。我的问题是,当我将代码提交到必须从标准输入读取的位置时,我无法弄清楚如何使代码工作。我看过其他几篇文章,但没有看到任何对我有意义的内容。我看到有些人只使用 raw_input - 但这会产生用户提示?不知道该怎么办。这是我的代码的一部分,应该读取输入并写入输出。关于这可能需要如何改变有什么建议吗?另外,一旦代码更改为从标准输入读取,我将如何测试代码?如何将测试数据放入标准输入?我从 Spotify 返回的错误显示运行时错误 - NameError。
import sys
# Read input
Input = []
for line in sys.stdin.readlines():
if len(line) <9:
teamCount = int(line)
if len(line) > 8:
subList = []
a = line[0:4]
b = line[5:9]
subList.append(a)
subList.append(b)
Input.append(subList)
##### algorithm here
#write output
print listLength
for empWin in win:
print empWin
I'm trying to solve the bilateral problem on Spotify's Tech Puzzles. http://www.spotify.com/us/jobs/tech/bilateral-projects/ I have something that is working on my computer that reads input from a file input.txt, and it outputs to ouput.txt. My problem is that I cannot figure out how to make my code work when I submit it where it must read from stdin. I have looked at several other posts and I don't see anything that makes sense to me. I see some people just use raw_input - but this produces a user prompt?? Not sure what to do. Here is the protion of my code that is suposed to read the input, and write the output. Any suggestions on how this might need changed? Also how would I test the code once it is changed to read from stdin? How can I put test data in stdin? The error i get back from spotify says Run Time Error - NameError.
import sys
# Read input
Input = []
for line in sys.stdin.readlines():
if len(line) <9:
teamCount = int(line)
if len(line) > 8:
subList = []
a = line[0:4]
b = line[5:9]
subList.append(a)
subList.append(b)
Input.append(subList)
##### algorithm here
#write output
print listLength
for empWin in win:
print empWin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你实际上做得还不错。
将从标准输入读取行。但是它可以缩短为:
我不使用 Windows,但要从命令行测试您的解决方案,您应该像这样运行它:
如果我像这样运行上面的代码,我会
偶然看到错误消息(因为我猜你没有发送该信息)是 Spotify 拼图检查器发现的错误。您可能只是在某个地方拼错了一个变量。
You are actually doing ok.
will read lines from stdin. It can however be shortened to:
I don't use Windows, but to test your solution from a command line, you should run it like this:
If I run your code above like that, I see the error message
which by accident (because I guess you didn't send in that) was the error the Spotify puzzle checker discovered. You have probably just misspelled a variable somewhere.