📖 Scenario: You are helping a friend who wants to understand how to make money by buying and selling stocks. They have a list of stock prices for several days and want to find the best way to buy and sell to maximize profit.
🎯 Goal: Build a simple program that finds the maximum profit from buying and selling stocks once, then extend it to multiple transactions, and finally print the results.
📋 What You'll Learn
Create a list called prices with exact daily stock prices
Create a variable called max_profit initialized to 0
Write a loop to find the maximum profit from one buy and one sell
Print the max_profit value
💡 Why This Matters
🌍 Real World
This project helps understand how to analyze stock prices to make smart buy and sell decisions for profit.
💼 Career
Knowing how to find maximum profit from stock prices is useful in finance, trading, and algorithmic problem solving.
Progress0 / 4 steps
1
Create the stock prices list
Create a list called prices with these exact values: [7, 1, 5, 3, 6, 4]
DSA Python
Hint
Use square brackets to create a list and separate numbers with commas.
2
Set up the max profit variable
Create a variable called max_profit and set it to 0
DSA Python
Hint
Use an integer variable to keep track of the best profit found so far.
3
Find the maximum profit from one transaction
Create a variable called min_price and set it to the first price in prices. Then use a for loop with variable price to go through each price in prices. Inside the loop, update min_price to the smaller of min_price and price. Also update max_profit to the larger of max_profit and price - min_price.
DSA Python
Hint
Keep track of the lowest price so far and calculate profit by subtracting it from the current price.
4
Print the maximum profit
Write a print statement to display the value of max_profit
DSA Python
Hint
The maximum profit is the highest difference between a later price and an earlier lower price.
Practice
(1/5)
1. What is the main goal when solving the Buy and Sell Stocks problem variants?
easy
A. Find the day with the lowest stock price only
B. Minimize the number of transactions regardless of profit
C. Maximize the profit by choosing the best days to buy and sell stocks
D. Calculate the average stock price over all days
Solution
Step 1: Understand the problem goal
The problem asks to find the best days to buy and sell stocks to get the highest profit.
Step 2: Identify the objective
Maximizing profit means buying low and selling high at the right times.
Final Answer:
Maximize the profit by choosing the best days to buy and sell stocks -> Option C
Quick Check:
Goal = Maximize profit [OK]
Hint: Focus on profit maximization, not just prices or counts [OK]
Common Mistakes:
Thinking the goal is to minimize transactions
Only finding lowest price day without selling
Calculating averages instead of profit
2. Which of the following Python code snippets correctly initializes variables to track minimum price and maximum profit for the stock problem?
easy
A. min_price = float('inf'); max_profit = 0
B. min_price = 0; max_profit = float('-inf')
C. min_price = None; max_profit = None
D. min_price = 0; max_profit = 0
Solution
Step 1: Understand initialization needs
We want min_price to start very high so any price is lower, and max_profit to start at zero.
Step 2: Check each option
min_price = float('inf'); max_profit = 0 sets min_price to infinity and max_profit to zero, which is correct for tracking minimum and maximum values.
Final Answer:
min_price = float('inf'); max_profit = 0 -> Option A
Quick Check:
Initialization = min_price high, max_profit zero [OK]
Hint: Use infinity for min_price start, zero for max_profit [OK]
Common Mistakes:
Setting min_price to zero causes wrong minimum tracking
Using None without checks causes errors
Setting max_profit to negative infinity can confuse logic
3. What is the output of the following code snippet for prices = [7,1,5,3,6,4]?
min_price = float('inf')
max_profit = 0
for price in prices:
if price < min_price:
min_price = price
elif price - min_price > max_profit:
max_profit = price - min_price
print(max_profit)