Spotify 技术难题 - Python 中的 stdin

发布于 2025-01-02 06:47:00 字数 866 浏览 4 评论 0原文

我正在尝试解决 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱她像谁 2025-01-09 06:47:00

你实际上做得还不错。

for line in sys.stdin.readlines():

将从标准输入读取行。但是它可以缩短为:

for line in sys.stdin:

我不使用 Windows,但要从命令行测试您的解决方案,您应该像这样运行它:

python bilateral.py < input.txt > output.txt

如果我像这样运行上面的代码,我会

Traceback (most recent call last):
  File "bilateral.py", line 20, in <module>
    print listLength
NameError: name 'listLength' is not defined

偶然看到错误消息(因为我猜你没有发送该信息)是 Spotify 拼图检查器发现的错误。您可能只是在某个地方拼错了一个变量。

You are actually doing ok.

for line in sys.stdin.readlines():

will read lines from stdin. It can however be shortened to:

for line in sys.stdin:

I don't use Windows, but to test your solution from a command line, you should run it like this:

python bilateral.py < input.txt > output.txt

If I run your code above like that, I see the error message

Traceback (most recent call last):
  File "bilateral.py", line 20, in <module>
    print listLength
NameError: name 'listLength' is not defined

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文