Created
September 23, 2020 05:28
-
-
Save marcoonroad/8db19cd6930bc2c1420eeb3054822e63 to your computer and use it in GitHub Desktop.
Missing Stock Prices (Hacker Rank challenge)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Challenge available at: | |
# https://www.hackerrank.com/challenges/missing-stock-prices/problem | |
import pandas | |
import numpy | |
count = int(input().strip()) | |
def normalize_value(value): | |
if value[0:7] == 'Missing': | |
return numpy.nan | |
else: | |
return float(value) | |
data = [] | |
missing = [] | |
for index in range(count): | |
[date, price] = input().split("\t") | |
data.append([ | |
date, | |
normalize_value(price) | |
]) | |
if price[0:7] == 'Missing': | |
[_, order] = price.split('Missing_') | |
entry = [ index, 1, int(order) ] | |
missing.append(entry) | |
else: | |
pass | |
date_series = [date for [date, _] in data] | |
price_series = [price for [_, price] in data] | |
dataframe = pandas.DataFrame({ | |
'date': pandas.Series(date_series), | |
'price': pandas.Series(price_series), | |
}) | |
dataframe = dataframe.assign( | |
DIdx=pandas.to_datetime( | |
dataframe.date, | |
format="%m/%d/%Y %H:%M:%S" | |
) | |
) | |
dataframe = dataframe.set_index('DIdx') | |
dataframe = dataframe.assign( | |
IPrice=dataframe.price.interpolate( | |
method='time' | |
) | |
) | |
result = [None for _ in range(len(missing))] | |
for [line, column, order] in missing: | |
value = dataframe.iloc[line, column + 1] | |
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