Have a go at it. The editor and the docs panel are open, and your code is saved as you type. Running it needs a free account — you’ll come back to exactly what you wrote.

Integer Division and Remainder

~8 mincode completion

Two operators come up constantly and are worth meeting on purpose:

17 // 5     # 3    floor division: how many whole 5s fit into 17
17 % 5      # 2    modulo: what is left over

Together they answer "how many whole groups, and how many left over". You will use % far more than you expect: checking whether a number is even is n % 2 == 0, and taking every 10th item is i % 10 == 0.

9 // 2      # 4      (not 4.5)
9 / 2       # 4.5
9 % 2       # 1

Careful with negatives: // rounds down, not toward zero, so -9 // 2 is -5.

Returning two values: a function can return several values at once by separating them with a comma. What comes back is a tuple, which you can unpack:

def split_name(full):
    parts = full.split(" ")
    return parts[0], parts[1]

first, last = split_name("Ada Lovelace")

Your task:

Write pack_boxes(items, box_size) that returns two values: how many full boxes you can fill, and how many items are left over.

With 17 items and boxes of 5, you fill 3 boxes and have 2 items left, so it returns (3, 2).

Example Tests

17 items in boxes of 5 fills 3 boxes with 2 left over

Input: {"items":17,"box_size":5}

Expected: [3,2]

An exact fit leaves nothing over

Input: {"items":20,"box_size":5}

Expected: [4,0]

Fewer items than a box means 0 full boxes and everything left

Input: {"items":3,"box_size":5}

Expected: [0,3]

Python
def pack_boxes(items, box_size):
    """
    Work out full boxes and leftovers.

    Args:
        items: total number of items (a whole number)
        box_size: how many items fit in one box (a whole number)

    Returns:
        A tuple (full_boxes, leftover).
    """
    # YOUR CODE HERE
    pass

Run your code to see results

⌘↵ runs against the visible tests

Loading docs…