Menu

Sign in to track your progress and unlock all features.

Theme style

Start learning freeLog in

Problem No 20

Compute Ticket Price with Multiple Rules

Hard

≈ 28 minute session

Summary

Compute the final ticket price applying multiple, ordered discounts and surcharges.

Problem statement

Write a function compute_ticket_price(age, is_student, day_of_week, early_bird) that calculates the final ticket price based on multiple rules. Pricing rules (apply in this order): 1. Base price is $20.0. 2. If age < 5, the ticket is free: return 0.0 immediately. 3. Apply either the student discount or the senior discount (they do not combine): - If is_student is True, apply a 50% discount on the current price. - Else if age >= 65, apply a 30% discount on the current price. - Otherwise no percentage discount here. Student discount takes precedence over the senior discount when both would apply. 4. If the day_of_week (case-insensitive) is "wednesday", apply an additional 10% discount to the current price. 5. If the day_of_week is "saturday" or "sunday" (case-insensitive), add a fixed weekend surcharge of $5 to the current price. 6. If early_bird is True, subtract $2 from the current price. 7. Ensure the final price is not negative. Round the final price to 2 decimal places and return it as a float. Examples below show the step-by-step application of the rules.

Task

Practice multi-branch conditional logic and ordering of rules: precedence, cumulative percentage discounts, fixed surcharges, and floor rules.

Examples

Student on Wednesday with early bird

Input

compute_ticket_price(20, True, 'Wednesday', True)

Output

7.0

Explanation

Base 20 -> student 50% -> 10 -> wednesday 10% -> 9 -> early bird -2 -> 7. Final: 7.0

Input format

Arguments: age (int), is_student (bool), day_of_week (string), early_bird (bool).

Output format

A float representing the final ticket price rounded to 2 decimals.

Constraints

- Base price fixed at 20.0. - Follow rule order exactly (precedence matters). - day_of_week comparisons are case-insensitive. - Never return a negative price; minimum is 0.0. - Return a float rounded to 2 decimal places.

Samples

Sample input 0

compute_ticket_price(70, False, 'Sunday', False)

Sample output 0

19.0

Explanation 0

Base 20 -> senior 30% -> 14 -> weekend +5 -> 19. Final: 19.0

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.