A cinema prices tickets with the function below. Count each if and elif as one decision point; else counts as none.
def ticket_price(age, day, member): if age < 0: raise ValueError("age must be non-negative") if age < 12: price = 8.00 elif age >= 65: price = 9.00 else: price = 14.00 if day == "Tue": price = price - 3.00 if member: price = price * 0.9 return round(price, 2)
A developer applies Extract Method to the age-based pricing:
def base_price(age): if age < 12: return 8.00 elif age >= 65: return 9.00 return 14.00 def ticket_price(age, day, member): if age < 0: raise ValueError("age must be non-negative") price = base_price(age) if day == "Tue": price = price - 3.00 if member: price = price * 0.9 return round(price, 2)
Give the cyclomatic complexity MMM of the original ticket_price, of the new base_price and of the refactored ticket_price, each as a whole number. Then give the price in dollars, to two decimal places, that the refactored code returns for ticket_price(70, "Tue", True).
Unlock full course notes by signing up
Renaming a public method safely
A shared library exposes a public method calc() that several other teams' services call. You want to rename it to calculate_invoice_total() while preserving callers' return values, exceptions and side effects. All callers are known, and delegation is assumed to preserve this behaviour.
Follow the library's required rollout policy:
Confirm that the existing tests pass before editing code.
Moving the rounding boundary
A store applies a 10% discount to item prices recorded as nonnegative integer cents. A developer proposes the following Python change as a refactoring:
For these nonnegative values, // 10 divides by ten and discards any fractional part. Fill in both functions' returned totals for each basket. Enter exact integers in cents; do not apply any additional rounding.
An extraction that passed the tests
In Python, a developer refactors this checkout function:
into this version:
Every existing test calls checkout with coupon=False, and all of them pass. Consider a customer whose item prices sum to 40.00 dollars and who uses a coupon. Ignoring floating-point rounding, which statement is accurate?
Removing a temporary changes object sharing
A maintenance team proposes removing a temporary variable from a Python function that creates task queues. Task durations are stored as integer minutes. The proposed change is intended to preserve existing behavior.
The following client code runs once with each version, starting with fresh queues each time:
Calculate morning_total and backup_total for the original version, then calculate both totals for the proposed version. Enter all four results in minutes, to zero decimal places.