EOF Error in python Hackerrank
Asked Answered
S

5

5

Trying to solve a problem but the compiler of Hackerrank keeps on throwing error EOFError while parsing: dont know where is m i wrong.

#!usr/bin/python

b=[]
b=raw_input().split()
c=[]
d=[]
a=raw_input()
c=a.split()
f=b[1]
l=int(b[1])
if(len(c)==int(b[0])):          
    for i in range(l,len(c)):
        d.append(c[i])
        #print c[i]
    for i in range(int(f)):
        d.append(c[i])
        #print c[i]
for j in range(len(d)):
    print d[j],

i also tried try catch to solve it but then getting no input.

try:
    a=input()
    c=a.split()
except(EOFError):
    a=""

input format is 2 spaced integers at beginning and then the array

the traceback error is:

Traceback (most recent call last):
  File "solution.py", line 4, in <module>
    b=raw_input().split()
EOFError: EOF when reading a line
Sloatman answered 28/3, 2017 at 18:29 Comment(11)
Youy should post the full traceback message.... Also, remove the last comma at print d[j],.Divulsion
@dot.Py, if the OP's using Python 2.x, the trailing comma is OK.Extra
, comma is for printig out on same line @DivulsionSloatman
@Extra i have added itSloatman
@Sloatman this is new to me, thanks for the info!Divulsion
Are you sure the input is coming from stdin? I haven't used Hackerrank but other sites usually ask you to write a function.Cadmus
Probably you are not passing any value to stdin as @ElmarPeise says, please check!Showily
Yup! I just did cross-check, either you let the website provide the input or give your input in the text box with proper format.Showily
hackerrank.com/challenges/array-left-rotation here is the question link to itSloatman
it doesnot says anything about how the input is being given @ElmarPeise have a look hackerrank.com/challenges/array-left-rotationSloatman
Please check this out for the correct answer: https://mcmap.net/q/1981812/-eof-error-in-python-hackerrankUther
S
0

i dont know but providing a custom input and compiling it and got me in! and passed all cases without even changing anything.

Sloatman answered 28/3, 2017 at 19:48 Comment(0)
A
5

There are several ways to handle the EOF error.

1.throw an exception:

while True:
  try:
    value = raw_input()
    do_stuff(value) # next line was found 
  except (EOFError):
    break #end of file reached

2.check input content:

while True:
  value = raw_input()
  if (value != ""):
    do_stuff(value) # next line was found 
  else:
    break 

3. use sys.stdin.readlines() to convert them into a list, and then use a for-each loop. More detailed explanation is Why does standard input() cause an EOF error

import sys 

# Read input and assemble Phone Book
n = int(input())
phoneBook = {}
for i in range(n):
    contact = input().split(' ')
    phoneBook[contact[0]] = contact[1]

# Process Queries
lines = sys.stdin.readlines()  # convert lines to list
for i in lines:
    name = i.strip()
    if name in phoneBook:
        print(name + '=' + str( phoneBook[name] ))
    else:
        print('Not found')
Atheling answered 1/8, 2017 at 19:34 Comment(1)
Please check this out for the correct answer: https://mcmap.net/q/1981812/-eof-error-in-python-hackerrankUther
P
2

I faced the same issue. This is what I noticed. I haven't seen your "main" function but Hackerrank already reads in all the data for us. We do not have to read in anything. For example this is a function def doSomething(a, b):a and b whether its an array or just integer will be read in for us. We just have to focus on our main code without worrying about reading. Also at the end make sure your function return() something, otherwise you will get another error. Hackerrank takes care of printing the final output too. Their code samples and FAQs are a bit misleading. This was my observation according to my test. Your test could be different.

Passerby answered 28/10, 2017 at 16:41 Comment(1)
Please check this out for the correct answer: https://mcmap.net/q/1981812/-eof-error-in-python-hackerrankUther
A
1

It's because your function is expecting an Input, but it was not provided. Provide a custom input and try to compile it. It should work.

Anibalanica answered 1/3, 2020 at 17:27 Comment(1)
yes exactly what i said and provided the answer on the same day.Sloatman
S
0

i dont know but providing a custom input and compiling it and got me in! and passed all cases without even changing anything.

Sloatman answered 28/3, 2017 at 19:48 Comment(0)
U
0

There are some codes hidden below the main visible code in HackerRank.

You need to expand that (observe the line no. where you got the error and check that line by expanding) code and those codes are valid, you need to match the top visible codes with the hidden codes.

For my case there was something like below:

regex_integer_in_range = r"___________" # Do not delete 'r'.
regex_alternating_repetitive_digit_pair = r"__________" # Do not delete 'r'.

I just filled up the above blank as like below and it was working fine with the given hidden codes:

regex_integer_in_range = r"^[0-9][\d]{5}$"  # Do not delete 'r'.
regex_alternating_repetitive_digit_pair = r"(\d)(?=\d\1)"   # Do not delete 'r'.
Uther answered 27/8, 2020 at 19:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.