Coding Quiz - Maximize Stock Trading Profit
In this coding quiz, you are tasked with writing a function to determine the most profitable buy and sell days from a given list of stock prices.
You will receive a list of stock prices as input from the user, and your function should calculate and return the maximum profit obtainable from a single transaction.
The stock must be bought before it is sold, and buying and selling cannot take place on the same day.
For example, given the stock prices [7, 1, 5, 3, 6, 4],
buying at the lowest price of 1 and selling at 6 results in a profit of 5. Therefore, the maximum profit is 5.
def solution(prices): # Write your code here return
Constraints
-
The length of the input list will be at least 2.
-
All stock prices are positive integers.
Input and Output Examples
-
Input:
[7, 1, 5, 3, 6, 4] -
Output:
5
-
Input:
[7, 6, 4, 3, 1] -
Output:
0
Lessons in this chapter · Python Coding Quiz Practice
- 1. Coding Quiz - Palindrome Check
- 2. Checking for Palindrome Strings - Problem Solving
- 3. Coding Quiz - Find Unique Words
- 4. Finding Unique Words - Problem Solving
- 5. Coding Quiz - Caesar Cipher
- 6. Caesar Cipher - Problem Solving
- 7. Coding Quiz - Find Prime Numbers
- 8. Finding Primes - Problem Solving
- 9. Coding Quiz - Finding Indices for a Target Sum
- 10. Creating Target Value with Two-Sum - Problem Solution
- 11. Coding Quiz - Maximize Stock Trading Profit
- 12. Finding Maximum Profit - Solution Explanation
- 13. Coding Quiz - Detecting Anagrams
- 14. Anagram Checker - Problem Solution
- 15. Coding Quiz - Remove the Smallest Number
- 16. Removing the Smallest Number - Problem Solution
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help