Skip to content

Instantly share code, notes, and snippets.

@marcoonroad
Last active September 21, 2020 23:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcoonroad/7f1c51f5fccb31db6c087acd97c19e1f to your computer and use it in GitHub Desktop.
Save marcoonroad/7f1c51f5fccb31db6c087acd97c19e1f to your computer and use it in GitHub Desktop.
Temperature Prediction (Hacker Rank challenge)
#!/usr/bin/env python3
# Challenge available at:
# https://www.hackerrank.com/challenges/temperature-predictions/problem
import pandas
import numpy
count = int(input().strip())
input()
months = {
'Jan': '1',
'Feb': '2',
'Mar': '3',
'Apr': '4',
'May': '5',
'Jun': '6',
'Jul': '7',
'Aug': '8',
'Sep': '9',
'Oct': '10',
'Nov': '11',
'Dec': '12',
}
def normalize_value(value):
if value[0:7] == 'Missing':
return numpy.nan
else:
return float(value)
data = []
missing = []
for index in range(count):
[year, month, tmax, tmin] = input().split("\t")
date = months[month[0:3]] + "/1/" + year
data.append([
date,
normalize_value(tmax),
normalize_value(tmin)
])
if tmax[0:7] == 'Missing':
[_, order] = tmax.split('Missing_')
entry = [ index, 1, int(order) ]
missing.append(entry)
else:
pass
if tmin[0:7] == 'Missing':
[_, order] = tmin.split('Missing_')
entry = [ index, 2, int(order) ]
missing.append(entry)
else:
pass
date_series = [date for [date, _, _] in data]
tmax_series = [tmax for [_, tmax, _] in data]
tmin_series = [tmin for [_, _, tmin] in data]
dataframe = pandas.DataFrame({
'date': pandas.Series(date_series),
'tmax': pandas.Series(tmax_series),
'tmin': pandas.Series(tmin_series),
})
dataframe = dataframe.assign(
DIdx=pandas.to_datetime(
dataframe.date,
format="%m/%d/%Y"
)
)
dataframe = dataframe.set_index('DIdx')
dataframe = dataframe.assign(
IMax=dataframe.tmax.interpolate(
method='time'
)
)
dataframe = dataframe.assign(
IMin=dataframe.tmin.interpolate(
method='time'
)
)
result = [None for _ in range(len(missing))]
for [line, column, order] in missing:
value = dataframe.iloc[line, column + 2]
result[order - 1] = float(str(value))
for value in result:
print(value)
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment