Exercise: Price After Discount in Python


We created two variables to store the original price of a product in dollars and the discount percentage applied to that price.

Use these two variables and arithmetic operations to compute and print the price after the discount.


Example:

For example, if price was 150 and discount was 10, the answer would be 135.

Why? Because the discount is 10%. 10% of 150 dollars is 15 dollars. And 150 - 15 dollars is 135 dollars.

Understanding the Problem

The core challenge of this problem is to correctly apply a percentage discount to a given price. This is a common task in various applications such as e-commerce, retail, and financial calculations. A potential pitfall is misunderstanding the percentage calculation or incorrectly applying the arithmetic operations.

Approach

To solve this problem, we need to follow these steps:

  1. Calculate the discount amount by multiplying the original price by the discount percentage divided by 100.
  2. Subtract the discount amount from the original price to get the final price after the discount.

Let's break down the steps:

  discount_amount = (price * discount) / 100
  final_price = price - discount_amount

This approach is straightforward and efficient with a time complexity of O(1) since it involves a constant number of arithmetic operations.

Algorithm

Here is a step-by-step breakdown of the algorithm:

  1. Read the original price and discount percentage.
  2. Calculate the discount amount using the formula: discount_amount = (price * discount) / 100.
  3. Subtract the discount amount from the original price to get the final price: final_price = price - discount_amount.
  4. Print the final price.

Code Implementation

# Define the original price and discount percentage
price = 150
discount = 10

# Calculate the discount amount
discount_amount = (price * discount) / 100

# Calculate the final price after discount
final_price = price - discount_amount

# Print the final price
print(f"The price after a {discount}% discount is: ${final_price}")

Complexity Analysis

The time complexity of this solution is O(1) because it involves a fixed number of arithmetic operations regardless of the input values. The space complexity is also O(1) as we are using a constant amount of extra space for the variables.

Edge Cases

Consider the following edge cases:

Examples:

  price = 100, discount = 0 -> final_price = 100
  price = 100, discount = 100 -> final_price = 0
  price = 0, discount = 50 -> final_price = 0

Testing

To test the solution comprehensively, consider the following test cases:

Using a testing framework like unittest in Python can help automate these tests.

Thinking and Problem-Solving Tips

When approaching such problems, it's important to:

Conclusion

In this blog post, we discussed how to calculate the price after a discount using basic arithmetic operations. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial in various real-world applications, and practicing these skills can significantly enhance your problem-solving abilities.

Additional Resources

For further reading and practice, consider the following resources: