Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added my solution #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
PLEASE FILL IN THE FOLLOWING!

## Full Name
John Johnson

Jose Renzo Delos Santos

## UWindsor Email
[email protected]

[email protected]

## Application Form
- [ ] I certify I have submitted the [application form](https://forms.office.com/r/R4A1JyB3Xf)

## Briefly explain how your solution works and how to run it
- [x] I certify I have submitted the [application form](https://forms.office.com/r/R4A1JyB3Xf)

## Briefly explain how your solution works and how to run it

My solution initializes the first element in the array as the lowest price. Then it iterates through the array, getting the profit as the current price - known lowest price. The profit is saved if it is the highest known profit. If the current price is lower than the known lowest price, the known lowest price gets updated to the current price. After the entire array has been iterated through, it return the max profit as an integer.

I also added comments alongside my code for better explanation.

My solution...
You can run my solution by going to the terminal, navigating to the folder containing my solution, running "python ./solution.py", and entering an array of numbers with spaces as delimeters to separate each number. Hopefully, my solution will print an integer showing the max profit.
44 changes: 44 additions & 0 deletions solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
def getMaxP(stock):
"""
A function that will return the max profit as an integer
"""
low, maxP = stock[0], 0
# low is the current lowest point from what has been iterated through or "seen"
# it signifies the buying point "buy low" and is initialized as the first element in the array
# maxP is the max profit and is initialized as 0

for price in stock:
# price is the price of the stock at the current iteration or "the price of the stock today"
# iterate through the entire array
if price < low:
# compare the price today vs the lowest recorded price,
# if the price of today is lower, it is not profitable to sell,
low = price
# so, you change the value of low to price today
else:
# else if price today is greater than or equal the lowest price,
# it is possible to earn a profit
profit = price - low
# profit today will be the price today minus the lowest recorded price
maxP = max(maxP, profit)
# compare whether the profit today is larger than the highest profit in the past
# make the higher one the max profit

return maxP
# return the max profit as an integer


if __name__ == "__main__":
# standard python driver code just to make sure this file is ran as the main file and not as an imported module

lst = str(input()).split()
# get the input from the user as a string of integers separated by spaces
# turn that string into an array using the split() method with spaces as the delimeter
# assign that array to a variable named lst
stock = [int(i) for i in lst]
# since the input is originally a string, lst is an array of strings
# using list comprehension, turn the elements of lst into integers
# asssign the new integer array to the variable stock

print(getMaxP(stock))
# call the function getMaxP and print the result