Home > Mobile >  Inputting a string array in python on hackerrank
Inputting a string array in python on hackerrank

Time:11-19

I am practicing a sample test on an online platform hackerrank.

Task: A time series of daily readings of mercury levels in a river is provided to you. In each test case, the day’s highest level is missing for certain days. By analyzing the data try to identify the missing mercury levels for those days. Each row of data contains two tab-separated values, a time-stamp and the day’s highest reading.

I have written my code in my own ide which produces the output in the format they require using an input.txt file as an input.

    data = pd.read_csv('../input.txt',skiprows = 1,sep="\t", header = None, names=['date','temp'])
    
    data['date'] = pd.to_datetime(data.loc[:,'date']).dt.date
    data['date'] = pd.to_datetime(data.loc[:,'date'], format= '%Y-%m-%d')
    data.temp = pd.to_numeric(data.temp, errors='coerce')
    
    datai = data.set_index('date')
    
    missingidx = datai.loc[datai['temp'].isna()].index
    ans = datai.temp.interpolate(method="polynomial", order=3).round(2)
    for i in missingidx:
        print(ans[i])
..
    print (ans)
...
 
Out:
32.57
32.02
32.5
...
28.14
26.87
27.42    
    

hackerrank is asking me to define the calcMissing function which "accepts String_Array readings as a parameter",

def calcMissing(readings):
# your code

if __name__ == "__main__":

There is also a custom input field where I can copy and paste the contents of my input.txt file. here's a screenshot.enter image description here

In this context, how can I input my data as a string array? I am confused. there isn't a way to write an absolute path.

they say I can read from the STDIN input like this:

n = int(input(readings))
for i in range(n):
   a, b = input().strip().split(' ')  # tried splitting on '/t'
   print (int(a)   int(b))

but it does not work on my data, because of "ValueError: invalid literal for int() with base 10: '250 1/3/2012 16:00:00\t26.96 1/4/2012 16:00:00\t27.47 1/5/2012 16:00:00\t27.728 1/6/2012 16:00:00\t28.19 1/9/2012.."

I have also found this code on stackoverflow in swift addressing a similar problem. maybe somebody knows how to rewrite it in python? my data's original format is 'object'.

let n = Int(readLine()!)! // Number of test cases
for _ in 1 ... n { // Loop from 1 to n
    let line = readLine()!.characters.split(" ").map{ String($0) } // Read a single line, do something with input
}

So my question is how I can adopt my code from above to use it in the calcMissing(readings) function in hackerrank?
It shows ''~ no response on stdout ~' in Output so I have no clue whats wrong.

CodePudding user response:

Notice this line of template code:

if __name__ == "__main__":

By default it is collapsed, but when you expand it, you can see how this template code reads the input file into a structured variable, like a list or a data frame. I couldn't find this particular code challenge on Hacker Rank, so I couldn't see for myself. But it is easy to check for yourself.

This data structure is then passed as argument to your function. So it is certain that you don't have to read an input file. The data is all there in the argument that is passed to your function. Just analyse that main code to see what the data structure is that is passed to your function.

  • Related