Standard 4

Receipt lines

order maps item names to (quantity, price) tuples. Write receipt(order) that returns one line per item, alphabetical by item, in the form <item> x <quantity> = <total>.

receipt({"milk": (2, 2.5), "bread": (3, 25)})  ->  ["bread x 3 = 75", "milk x 2 = 5.0"]
def receipt(order):
    ...

Is it ascending?

Write is_ascending(nums) that returns True if each number is greater than or equal to the one before it. Empty and single-item lists count as sorted.

is_ascending([1, 2, 2, 5])  ->  True
is_ascending([3, 1])        ->  False
def is_ascending(nums):
    ...

Remove the vowels

Write no_vowels(text) that returns the text with every lowercase vowel removed.

no_vowels("sourdough bread")  ->  "srdgh brd"
def no_vowels(text):
    ...

Lowest and highest

Write price_range(prices) that returns a tuple (lowest, highest) from a non-empty list of prices.

price_range([40, 2.5, 25])  ->  (2.5, 40)
def price_range(prices):
    ...

Group by first letter

Write by_letter(items) that returns a dictionary mapping each first letter to the list of items starting with it, in their original order.

by_letter(["apple", "bread", "apricot"])  ->  {"a": ["apple", "apricot"], "b": ["bread"]}
def by_letter(items):
    ...

Do and undo

actions is a list of strings. "undo" cancels the most recent action that has not already been undone; anything else is an action to do. Write final(actions) that returns the list of actions still in effect, in order. An "undo" with nothing to undo is ignored.

final(["add bread", "add milk", "undo", "add eggs"])  ->  ["add bread", "add eggs"]
final(["undo", "add milk"])                           ->  ["add milk"]
def final(actions):
    ...

Most urgent few

orders is a list of (priority, name) tuples with distinct priorities. Write urgent(orders, k) that returns the names of the k most urgent orders, most urgent first. k may exceed the number of orders.

urgent([(3, "bread"), (1, "cake"), (2, "rolls")], 2)  ->  ["cake", "rolls"]
import heapq

def urgent(orders, k):
    ...

Stock value

stock maps items to quantities and prices maps items to unit prices. Write stock_value(stock, prices) that returns the total value of the stock, counting only items that have a price.

stock_value({"apple": 3, "bread": 2, "gift": 1}, {"apple": 40, "bread": 25})  ->  170
def stock_value(stock, prices):
    ...

Just the number

Product codes mix letters, dashes and digits. Write code_number(code) that returns the digits of the code, in order, as a single whole number. Every code has at least one digit. Leading zeros disappear, as they do for any number.

code_number("BRD-1042-A")  ->  1042
code_number("x7y")         ->  7
code_number("A007")        ->  7
def code_number(code):
    ...

Above average

Write above_average(prices) that returns how many prices in a non-empty list are strictly greater than the average of the list.

above_average([40, 25, 2.5, 60])  ->  2
above_average([5, 5, 5])          ->  0
def above_average(prices):
    ...

Next: Stretch 1.