Apply Discount Every n Orders
The problem asks to implement a class Cashier
that simulates the billing system at a supermarket with a discount applied to every nth customer. We’ll implement this class using two functions:
__init__
: Initializes the object with the given values.getBill
: Returns the total bill after applying any applicable discounts.
Here’s the code:
|
|
Key Takeaways
- Storing Products and Prices: We store the products and prices as a dictionary to make the lookup efficient.
- Discount Application: A discount is applied only when the current customer count is divisible by
n
. - Time Complexity: Both the initialization and the
getBill
function work in O(1) time for each call, except for the sum operation insidegetBill
, which takes O(p) time, where p is the length of the product list for that particular call. Hence, the overall time complexity for each call togetBill
is O(p). - Space Complexity: We are storing the products and prices in a dictionary, so the space complexity is O(p), where p is the number of products.