1

I recently participated in hackathon for the first time and got stuck on the first problem. I solved the algorithm, but couldn't figure out how to take values from stdin using Python. This is the question:

There are two college students that want to room together in a dorm. There are rooms of various sizes in the dormitory. Some rooms can accomodate two additional students while others cannot.

Input: the first input line will be a number n (1 ≤ n ≤ 100), which is the total number of rooms in the dorm. There will be n lines following this, where each line contains two numbers, p and q (0 ≤ p ≤ q ≤ 100). P is the number students already in the room, while q is the maximum number of students that can live in the room.

Output: print the number of rooms that the two students can live in.

This is my solution. I've tested it using raw_input() and it works perfectly on my interpreter, but when I change it to just input() I get an error message.

def calcRooms(p, q):
    availrooms = 0
    if q - p >= 2:
        availrooms += 1
    return availrooms

def main():
    totalrooms = 0
    input_list = []

    n = int(input())
    print n

    while n > 0:
        inputln = input().split() #accepts 2 numbers from each line separated by whitespace.
        p = int(inputln[0])
        q = int(inputln[1])
        totalrooms += calcRooms(p, q)
        n -= 1

    return totalrooms

print main()

The error message:

SyntaxError: unexpected EOF while parsing

How do I accept data correctly from stdin?

2
  • 1
    raw_input Commented Sep 27, 2014 at 19:06
  • 1
    I like to use sys.stdin.readline() or "for line in sys.stdin:". Then you can "line.split()" and convert to int or whatever. input() works in CPython 2.x, but it's kind of insecure. In 3.x, I believe input() is fine. In 2.x, you have to use raw_input() and remember not to use input(). But I still prefer to use sys.stdin. Commented Sep 27, 2014 at 19:12

1 Answer 1

3

In this particular case, use raw_input to take the entire line as string input.

inputln = raw_input().split()

This takes input line as a string and split() method splits the string with space as delimiter and returns a list inputln

The following code works the way you wanted.

def main():
    totalrooms = 0
    input_list = []
    #n = int(input("Enter the number of rooms: "))
    n = input()

    while n > 0: # You can use for i in range(n) :
        inputln = raw_input().split() #Converts the string into list

        p = int(inputln[0]) #Access first element of list and convert to int
        q = int(inputln[1]) #Second element

        totalrooms += calcRooms(p, q)
        n -= 1

    return totalrooms

Or, alternatively you may use fileinput.

If input file is not passed as command line argument, stdin will be the default input stream.

import fileinput
for line in fileinput.input() :
      #do whatever with line : split() or convert to int etc

Please refer : docs.python.org/library/fileinput.html

Hope this helps, drop comments for clarification if needed.

Sign up to request clarification or add additional context in comments.

3 Comments

So, raw_input() will work with stdin? I have to submit my answer to the hackathon website where they will test the code. There is no one to enter numbers at the command line. Just wanted to be sure because I've submitted the wong solution 4 times already and it's starting to get embarassing!
Yes, raw_input() will work with stdin. Usually the test cases in hackathon or online judges will be input files having series of test cases. The above code should work.
I followed your explanation and the code submitted correctly at the hackathon website. Thanks so much!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.