Standard 1
Line totals
lines is a list of (item, quantity, price) tuples. Write
line_totals(lines) that returns a list of the total for each line.
line_totals([("bread", 3, 25), ("milk", 2, 2.5)]) -> [75, 5.0]
def line_totals(lines):
...
Items over a price
prices maps item names to prices. Write over(prices, limit) that
returns the names of items priced strictly above limit, in alphabetical order.
over({"apple": 40, "milk": 2.5, "bread": 25}, 20) -> ["apple", "bread"]
def over(prices, limit):
...
Initials
Write initials(name) that returns the first letter of each word, in
capitals, joined together. The name has at least one word.
initials("ada lovelace") -> "AL"
initials("grace brewster hopper") -> "GBH"
def initials(name):
...
Longest word
sentence has at least one word, separated by spaces. Write
longest_word(sentence) that returns its longest word. On a tie, return
the first.
longest_word("fresh sourdough bread") -> "sourdough"
def longest_word(sentence):
...
Stock after sales
stock maps items to quantities and sales is a list of item names, one
per item sold. Write after_sales(stock, sales) that reduces each sold
item's quantity by one, changing stock itself, and returns it. Every sold
item is in stock.
after_sales({"apple": 3, "bread": 2}, ["apple", "bread", "apple"]) -> {"apple": 1, "bread": 1}
def after_sales(stock, sales):
...
Unique, in order
Write unique(items) that returns a new list with duplicates removed,
keeping the first occurrence of each item and the original order.
unique(["apple", "bread", "apple", "milk", "bread"]) -> ["apple", "bread", "milk"]
def unique(items):
...
Palindrome, ignoring case and spaces
Write is_palindrome(text) that returns True if the text reads the same
backwards, ignoring capital letters and spaces.
is_palindrome("Was it a cat I saw") -> True
is_palindrome("bread") -> False
def is_palindrome(text):
...
Serve some customers
arrivals is a list of names in arrival order. Write remaining(arrivals, n)
that serves the first n customers from a deque and returns a list of who
is still waiting. If n is more than the number of customers, nobody is
left. Leave arrivals itself unchanged.
remaining(["Ada", "Grace", "Linus"], 2) -> ["Linus"]
remaining(["Ada"], 5) -> []
from collections import deque
def remaining(arrivals, n):
...
Letter counts
Write letter_counts(text) that returns a dictionary mapping each
character in text to how many times it appears, ignoring spaces.
letter_counts("a bad cab") -> {"a": 3, "b": 2, "d": 1, "c": 1}
def letter_counts(text):
...
Capitalise each word
Write title_case(text) that capitalises the first letter of every word
and lowercases the rest. Words are separated by single spaces.
title_case("fresh SOURDOUGH bread") -> "Fresh Sourdough Bread"
def title_case(text):
...
Next: Standard 2.