0

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

1 Answer 1

0

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.

1
  • ok thanks for the help. I was having a hard time figuring out how to that! I didn't want to post my full solution to the puzzle so I left out the middle where my variables were defined - oops
    – John
    Feb 6, 2012 at 23:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.