Last active
January 28, 2024 07:42
-
-
Save jedavidson/0081e7c4ac9985eb5ed24d0665492eb7 to your computer and use it in GitHub Desktop.
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
| #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 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
| 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] |
Author
Author
Other nice financial-themed problems:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample solution using the
interpfunction from NumPy:One note about this solution compared to the last one is that
np.interpwill simply return the first/last price for times before/after the range of given information. For example, att = 21the price returned will be113.00instead of115.00.