mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-23 00:41:19 -04:00
Change the dir structure slightly
This commit is contained in:
parent
6b6fe21db3
commit
2f4a9638c0
184 changed files with 0 additions and 21 deletions
54
source_code/builtin_structures/max_subarray_stocks.py
Executable file
54
source_code/builtin_structures/max_subarray_stocks.py
Executable file
|
@ -0,0 +1,54 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
|
||||
def beating_stock(array):
|
||||
|
||||
imin = 0
|
||||
|
||||
# first deal is just buying in the next day (1)
|
||||
deal = [array[1] - array[imin], imin, 1]
|
||||
|
||||
for i, d in enumerate(array):
|
||||
|
||||
deal_here = d - array[imin]
|
||||
|
||||
if deal_here > deal[0]:
|
||||
deal = [deal_here, imin, i]
|
||||
|
||||
elif d < array[imin]:
|
||||
imin = i
|
||||
|
||||
return deal[0], array[deal[1]], array[deal[2]]
|
||||
|
||||
|
||||
def beating_stock2(array):
|
||||
|
||||
deal = 0
|
||||
min_value = array[0]
|
||||
|
||||
for i, d in enumerate(array):
|
||||
|
||||
deal_here = d - min_value
|
||||
|
||||
if deal_here > deal:
|
||||
deal = deal_here
|
||||
|
||||
else:
|
||||
if min_value > array[i]:
|
||||
min_value = array[i]
|
||||
|
||||
return deal
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
array = [7, 2, 3, 6, 5, 8, 5, 3, 4]
|
||||
|
||||
deal = beating_stock(array)
|
||||
print("Profit: %d, buying at %d, selling at %d." %(deal[0], deal[1], deal[2]))
|
||||
|
||||
deal = beating_stock2(array)
|
||||
print "Profit: " + str(deal)
|
Loading…
Add table
Add a link
Reference in a new issue