Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EOF Error in Python 2

I am solving a problem on http://hackerrank.com using Python 2

The compiler is giving an error

Traceback (most recent call last): File "/run-Lx3mHJ3G2jHRLRW9bjbX/solution.py", line 4, in t = raw_input() EOFError: EOF when reading a line

This is the code :

import sys
a = []
while 1:
    t = raw_input()
    if t=="":
        break
    else:
        s = [i for i in t]
        s.reverse()
        a.append(s)

a.reverse()
for i in a:
    for j in i:
        sys.stdout.write(j)
    sys.stdout.write('\n')

When I run it on my computer, it works fine.

Is it a problem I should report with the HackerRank interpreter or am I doing something wrong?

For sake of complete information, I already tried using "input()", "str(input())" and other possible variants.

like image 986
Cheeku Avatar asked Feb 25 '26 02:02

Cheeku


1 Answers

HackerRank seems not to support the python idiom of repeating raw_input() until it gets an empty line. HackerRank apparently requires the submitted code to use the test description parameters in the header section (first line or two of input) to control the number of lines read.

Attempting to read past the last expected input line triggered a similar EOFError in my trials:

...
def main():
    lines = []
    line = raw_input()
    while line:
        lines.append(line)
        line = raw_input()    # line 232
    ...

resulted in

Status: EOFError thrown on line 232

Rewriting the input code to read just the expected number of lines was enough for the revised submission to pass. For example, for the 'Service Lane' warmup exercise in the Algorithms section:

...
first_line = raw_input()
freeway_length, testcase_count = parse_session_controls(first_line)

second_line = raw_input()
widths = parse_widths(second_line, freeway_length)

for _unused in range(testcase_count):
    testcase_line = raw_input()
    entrance_num, exit_num = parse_testcase(testcase_line, freeway_length)
    print(measure_bottleneck(widths, entrance_num, exit_num))
...
like image 144
user3534671 Avatar answered Feb 28 '26 11:02

user3534671