-1

I am trying out this problem in a coding competition. I believe I have solved the problem, but have some problem in taking the input. Help me out here:


Input

The first line of the input contains a single integer T denoting the number of test cases. The description for T test cases follows. Each test case consists of a single line containing two space-separated strings R and S denoting the two recipes.


Now, I have coded the problem and it seems to work, but whenever I directly copy paste the input values, it fails to work by giving this error message


T= int(raw_input())
ValueError: invalid literal for int() with base 10:
'3\nalex axle\nparadise diapers\nalice bob'

Whenever I try to submit the problem, I get an error message. May be they are also copy pasting the input values and checking for the output. My code skeleton goes something like this


def whetherGranama(str1,str2):
    return "NO"
    #can't give the implementation out yet

T= int(raw_input())
ans=[]
for x in range(0,T):
    s=raw_input()
    s1,s2=s.split()
    ans.append(whetherGranama(s1,s2))

for elem in ans:
    print elem

How can I fix the \n error ? I think the entire input is treated as one string.

4 Answers 4

2

Split the input, extract the integer the process using the split list

s = raw_input()
s = s.split()
T = int(s[0])

ans=[]

for st in s[1:]:
//Do the rest 
Sign up to request clarification or add additional context in comments.

Comments

1

If the entire input is being read in as one string, you could try using stdin.readline() instead of raw_input to capture the input stream:

from sys import stdin
T = int(stdin.readline())

Since this is a coding competition however, I'm assuming that speed is of the essence. Since IO operations are computationally expensive, you should actually welcome the opportunity to read all of your input at one time. In other words, it's generally faster to read it in all at once and then parse the input within your code. I guess in your case, it would look something like this (assuming that it comes in all at once by design):

data = raw_input().splitlines() 
#(or data = sys.stdin.read().splitlines() or data = list(sys.stdin.readlines()))

T = int(data[0])
S = (s.split() for s in data[1:])

Comments

1

Split your input first and then convert the int:

T, body = raw_input().split("\n", 1)
for x in xrange(int(T)):
   ...

That will split once and give you the first number item and then the rest of your input string.

Comments

1

Yes the entire string is treated as one input. You can simply store the input as a list and work with the list instead of calling raw_input in your loop, that would look something like this:

def whetherGranama(str1,str2):
    return "NO"
    #can't give the implementation out yet

input_lines = raw_input().split("\n")
T = int(input_lines[0])
ans=[]
for x in range(1,T):
    s = input_lines[x]
    s1,s2=s.split()
    ans.append(whetherGranama(s1,s2))

for elem in ans:
    print elem

Comments

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.