Problem 117190 · easy · Level 01 Prerequisites & Setup

Every Table Gets Its Own Order

default arguments · keyword arguments · None · dictionaries · py-functions · py-dicts

A café's tablet app builds orders one item at a time. An order is a dictionary from item name to quantity.

Write add_to_order(item, qty=1, order=None):

  • if an order is given, add qty of item to it (starting from 0 if the item is not in it yet), and return that same dictionary, so the caller sees the change;
  • if no order is given, start a new, empty order for this call, add the item to it and return it.

Every call without an order must get its own new dictionary: a second table's first item must never appear on the first table's order. An empty dictionary passed as order is a real (empty) order and must be the one that is filled and returned.

Examples

Input:  add_to_order("tea")
Output: {"tea": 1}

Input:  add_to_order("tea", order={"tea": 2, "bun": 1})
Output: {"tea": 3, "bun": 1}

Input:  (add_to_order("tea"), add_to_order("cake", 2))
Output: ({"tea": 1}, {"cake": 2})
Explanation: two calls without an order are two different tables.

Constraints

  • 1 <= qty <= 100
  • the tests call your function many times in a row, in one program

Goals

  • Use None as a default and create a fresh dictionary inside the function
  • Update a dictionary that the caller passed in, and return that same object
Starting Python…