lapieza.io - Assessment | Stock
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
8 | |
1 | |
4 9 5 2 6 7 8 1 8 | |
1 3 0 0 | |
0 0 1 1 3 | |
2 6 0 0 1 7 | |
2 6 1 0 1 7 1 1 3 4 | |
0 1 2 3 4 5 6 | |
0 1 2 0 1 6 |
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
0 | |
7 | |
2 | |
3 | |
7 | |
6 | |
6 | |
6 |
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
""" | |
lapieza.io - start up assessment | |
Github: @realFranco | |
July 21th, 2022 | |
# PDF File "stock.pdf" contains the explanation in Espanish about my solution. | |
# Instructions | |
- Standard input output | |
# To pipe the results into a file | |
pytohn3 stock.py < input > output | |
cat output | |
# To watch the result on comand line | |
pytohn3 stock.py < input | |
tests = [ | |
[4, 9, 5, 2, 6, 7, 8, 1, 8], # 7 | |
[1, 3, 0, 0], # 2 | |
[0, 0, 1, 1, 3], # 3 | |
[2, 6, 0, 0, 1, 7], # 6 | |
[2, 6, 1, 0, 1, 7, 1, 1 , 3, 4], # 7 | |
[0, 1, 2, 3, 4, 5, 6], # 6 (Big O Worst case) | |
[0, 1, 2, 0, 1, 6], # 6 | |
] | |
""" | |
import math | |
from typing import List | |
MAX = math.pow(10, 4) + 1 | |
MIN = -MAX | |
class Solution: | |
@staticmethod | |
def collect_revenue(stock: List[int], revenue_chances: List) -> List[ List[int] ]: | |
if len(stock) < 2: | |
return [] | |
n_max, n_min = MIN, MAX | |
i, n = 0, len(stock) | |
while i < n: | |
if stock[i] < n_min: | |
n_min_iter = i | |
n_min = stock[i] | |
n_max = MIN # Restart max | |
if n_max < stock[i]: | |
n_max_iter = i | |
n_max = stock[i] | |
# Making sure that buy happens before sell and prices are diffs. | |
if n_max_iter > n_min_iter and n_min < n_max: | |
revenue_chances.append([n_max, n_min]) | |
n_max, n_min = MIN, MAX | |
i -= 1 | |
i += 1 | |
return revenue_chances | |
if __name__ == '__main__': | |
n = int(input()) | |
for case in range(0, n): | |
stock = list(map(int, input().split())) | |
revenue_history = Solution.collect_revenue(stock=stock, revenue_chances=[]) | |
only_one_max = False | |
max_price, lower_price = MIN, MAX | |
profit = 0 | |
for commerce_action in revenue_history[::-1]: | |
if commerce_action[1] < lower_price: | |
lower_price = commerce_action[1] | |
if only_one_max == False and max_price < commerce_action[0]: | |
max_price = commerce_action[0] | |
only_one_max = True | |
# Collect local profit | |
if profit < commerce_action[0] - commerce_action[1]: | |
profit = commerce_action[0] - commerce_action[1] | |
profit = max_price - lower_price if profit < max_price - lower_price else profit | |
print(profit) |
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
> https://drive.google.com/file/d/1NZVqCK5DTfQmHAAn8_-CyDXg3OuJ91yM/view?usp=sharing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment