Search code examples
pythoninputstdin

Hackerrank Python Input


I'm having trouble with what seems like a basic challenge from HackerRank. It is a unit conversion problem at Unit Conversion, but I'm struggling with the basics: How do I read the input in Python? I already know that x = int(input()) reads the first line of input and that input.split() separates the input by whitespaces.

But in this particular challenge, there's multiple lines of input! How do I even begin to read all of those lines with Python? I can't possibly assign 7 variables to just read 7 lines! And some lines contain both strings and integers! How do I store the strings and integers in seperate lists? I'm so confused. Any help is greatly appreciated.

Problem:

The input will begin with a line containing 2 numbers x and y meaning that "x of unit A" is equal to "y of unit B". For instance, if A is "kilos" and B is "pounds" one possibility is x = 3.25 and y = 7.165024.

The next line will contain a single integer N, containing the number of conversions to be performed.

Each of the next N lines will be of the form "z q" where z is a number and q is either 'A' or 'B'.

Output N lines with a number each. See the sample input/output for further details.

Sample Input:

enter image description here

Sample Output:

enter image description here


Solution

  • In hackerrank, pair inputs are mostly space separated. Like in your case it's after 5, number letter. The cases will differ on the basis of test cases, 5 for now. But you do have three things that you know, x y & number. You could use the split function for getting x&y after reading the line, then create dictionary or two diff lists to store them in pairs.