Last active
January 28, 2024 07:42
-
-
Save jedavidson/0081e7c4ac9985eb5ed24d0665492eb7 to your computer and use it in GitHub Desktop.
This file contains 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
#include <cassert> | |
#include <vector> | |
// Given some price information at discrete time intervals, returns a suitable | |
// value for the price at a given time interval. You may assume that times is | |
// sorted in ascending order, and that there are at least two datapoints (i.e. | |
// prices for at least two times). | |
auto stock_price(int time, std::vector<int> const ×, | |
std::vector<float> const &prices) -> float { | |
return 0; | |
} | |
auto main() -> int { | |
// Some sample time and price data. You might want to call stock_price for | |
// different time arguments to test your function. | |
auto const times = std::vector<int>{1, 3, 6, 11, 17, 20}; | |
auto const prices = | |
std::vector<float>{100.00, 106.00, 109.00, 104.00, 107.00, 113.00}; | |
} |
This file contains 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
from typing import List | |
def stock_price(time: int, times: List[int], prices: List[float]) -> float: | |
""" | |
Given some price information at discrete time intervals, returns a suitable | |
value for the price at a given time interval. You may assume that times is | |
sorted in ascending order, and that there are at least two datapoints (i.e. | |
prices for at least two times). | |
""" | |
return 0 | |
if __name__ == "__main__": | |
# Some sample time and price data. You might want to call stock_price for | |
# different time arguments to test your function. | |
times = [ 1, 3, 6, 11, 17, 20] | |
prices = [100.00, 106.00, 109.00, 104.00, 107.00, 113.00] |
Sample solution using the interp
function from NumPy:
from typing import List
import numpy as np
def stock_price(t: int, times: List[int], prices: List[float]) -> float:
"""
Given some price information at discrete time intervals, return a suitable
value for the price at a given time interval. You may assume that times is
sorted in ascending order, and that there are at least two datapoints (i.e.
a price at at least two times).
"""
return np.interp(t, times, prices)
if __name__ == "__main__":
times = [ 1, 3, 6, 11, 17, 20]
prices = [100.00, 106.00, 109.00, 104.00, 107.00, 113.00]
for time, price in zip(times, prices):
assert stock_price(time, times, prices) == price
assert stock_price(2, times, prices) == 103.00
assert stock_price(4, times, prices) == 107.00
assert stock_price(5, times, prices) == 108.00
One note about this solution compared to the last one is that np.interp
will simply return the first/last price for times before/after the range of given information. For example, at t = 21
the price returned will be 113.00
instead of 115.00
.
Other nice financial-themed problems:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample solutions:
Worst-case time complexity is$O(\log{n})$ , dominated by the cost of binary searching
times
for the first time aftert
. Space complexity is constant.