Easy: Warm-Ups · Problem 17 of 82

Call Cost

EasyProblem SolvingPython

A phone company prices calls like this:

  • A call of up to 180 seconds (3 minutes) costs a flat 50 cents.
  • After that, every minute that is started costs 10 cents more.
  • A call of 0 seconds was never connected and costs nothing.

Write call_cost(seconds) that takes the length of a call in whole seconds and returns the price in cents.

Understand the problem

Before you write any code, make sure you can answer questions about the rules in your own words. Answer these three.

What should call_cost(180) return?

  1. 0
  2. 50
  3. 60

"Up to 180 seconds" includes 180, so the call is still in the flat part. The first extra minute is only charged once the call goes past 180 seconds.

What should call_cost(181) return? Type the number of cents.

Second 181 is the first second of a new minute, and "every minute that is started" is charged in full. So the price is 50 + 10 = 60. The word "started" is the one to underline in this problem.

What should call_cost(0) return?

The third rule says a 0-second call costs nothing. This is an edge case: the first two rules alone would give 50, so a solution that ignores the third rule is wrong for this one input.

Try examples by hand

Do the arithmetic yourself first. Then you can tell whether your code is wrong or your idea is.

A call lasts 241 seconds. After the first 180 seconds, how many started extra minutes are charged?

241 - 180 = 61 extra seconds. The first 60 seconds make one minute, and the 61st second starts a second minute. So 2 extra minutes, and the price is 50 + 2 x 10 = 70.

Which expression gives 2 for 70 extra seconds and 1 for exactly 60 extra seconds? (Assume import math.)

  1. 70 // 60
  2. round(70 / 60)
  3. math.ceil(70 / 60)
  4. 70 % 60

70 // 60 is 1, which drops the started minute. round(70 / 60) is also 1, because 1.17 rounds down. 70 % 60 is 10, the leftover seconds. Only ceil ("round up") gives 2 for 70 and still gives 1 for exactly 60. Rounding up is what "every minute that is started" means.

Write the solution

Write the function. The tests include inputs on and next to every boundary, because that is where mistakes hide.

def call_cost(seconds):
    # return the price in cents
    pass
Hint 1: a question to ask yourself

How many different "zones" does the input fall into? Where exactly does one zone end and the next begin?

Hint 2: the shape of the answer

Three cases, checked in this order: a call of 0 seconds, a call within the flat part, and a longer call. For the longer call, work out the extra seconds first, then the number of started minutes.

Reflect

Solution

import math

def call_cost(seconds):
    if seconds == 0:
        return 0
    if seconds <= 180:
        return 50
    extra_minutes = math.ceil((seconds - 180) / 60)
    return 50 + 10 * extra_minutes

Another way, without math: -(-x // 60) is "divide and round up" using whole-number division.

def call_cost(seconds):
    if seconds == 0:
        return 0
    return 50 + 10 * max(0, -(-(seconds - 180) // 60))

Pattern: list the boundaries, then test both sides of each. The rules have two boundaries (0 and 180) and a repeating step (every 60 seconds). The inputs worth trying are 0, 1, 180, 181, 240, 241: each is on a boundary or one step beside it. Most bugs in this kind of problem are off-by-one errors at a boundary, and you find them by testing exactly there.

Words that change the code: "up to" (includes the end), "started" (round up), "never connected" (a special case). Underline such words when you read the task.

Cost: a few comparisons and one division, so time and space are both constant, O(1).

What an interviewer might ask next: the company adds a cheaper night rate. How would you change the code so the prices live in one place instead of being spread through the if statements?