Compound data
# Google is encouraged. Type what you find, do not paste it.
# ---- Part 1: lists ----
def total(numbers):
"""Return the sum of a list of numbers.
total([1, 2, 3]) → 6
total([]) → 0
Search: "python sum list loop".
"""
raise NotImplementedError
def largest(numbers):
"""Return the largest number in the list.
largest([3, 9, 4]) → 9
largest([]) → None
Search: "python largest number in list loop".
"""
raise NotImplementedError
def evens(numbers):
"""Return a new list with the even numbers of numbers.
Build the result by starting from an empty list and appending.
evens([1, 2, 3, 4]) → [2, 4]
evens([1, 3]) → []
Search: "python list append".
"""
raise NotImplementedError
def squares(numbers):
"""Return a new list with the square of each number.
Build the result by starting from an empty list and appending.
squares([2, 3]) → [4, 9]
squares([]) → []
Search: "python list append".
"""
raise NotImplementedError
def remove_last(numbers):
"""Remove the last item of the list and return the list.
remove_last([1, 2, 3]) → [1, 2]
remove_last([]) → []
Search: "python list pop".
"""
raise NotImplementedError
# ---- Part 2: dicts ----
def lengths(words):
"""Return a dict mapping each word to its length.
Build the result by starting from an empty dict and assigning keys.
lengths(["tea", "coffee"]) → {"tea": 3, "coffee": 6}
lengths([]) → {}
Search: "python dictionary add key".
"""
raise NotImplementedError
def word_counts(text):
"""Return a dict mapping each word in text to how often it appears.
word_counts("a b a") → {"a": 2, "b": 1}
word_counts("") → {}
Search: "python count words dictionary".
"""
raise NotImplementedError
def lookup(prices, item):
"""Return the price of item from the dict prices.
lookup({"tea": 3.5}, "tea") → 3.5
lookup({}, "tea") → None
Search: "python dictionary get key".
"""
raise NotImplementedError
def set_price(prices, item, price):
"""Add or update the price of item in the dict prices, then return it.
set_price({}, "tea", 3.5) → {"tea": 3.5}
set_price({"tea": 3.0}, "tea", 4.0) → {"tea": 4.0}
Search: "python dictionary update value".
"""
raise NotImplementedError
def remove_price(prices, item):
"""Remove item from the dict prices and return the dict.
remove_price({"tea": 3.5, "jam": 4.0}, "tea") → {"jam": 4.0}
remove_price({}, "tea") → {}
Removing a missing item must not crash.
Search: "python dictionary remove key".
"""
raise NotImplementedError
# ---- Part 3: table ----
def table(rows):
"""Print an aligned table of (name, score) pairs with a header line.
table([("ada", 90), ("liam", 85)]) →
name score
ada 90
liam 85
Both columns are padded to a width of 10.
Search: "python f-string padding".
"""
raise NotImplementedError