Warm-up 1
Double the price
Write double_price(price) that returns twice the price.
double_price(40) -> 80
double_price(2.5) -> 5.0
def double_price(price):
...
Shout it
Write shout(text) that returns the text in capitals with an exclamation mark
added at the end.
shout("fresh bread") -> "FRESH BREAD!"
def shout(text):
...
Is the list empty?
Write is_empty(items) that returns True if the list has no items.
is_empty([]) -> True
is_empty(["bread"]) -> False
def is_empty(items):
...
Middle item
Write middle(items) that returns the item in the middle of a list with an
odd number of items.
middle(["apple", "bread", "milk"]) -> "bread"
middle([7]) -> 7
def middle(items):
...
Count the vowels
Write vowels(text) that returns how many characters of text are vowels:
a, e, i, o or u, lowercase only.
vowels("sourdough") -> 4
vowels("xyz") -> 0
def vowels(text):
...
Is it priced?
Write is_priced(prices, item) that returns True if item is a key in
the dictionary prices.
is_priced({"apple": 40}, "apple") -> True
is_priced({"apple": 40}, "milk") -> False
def is_priced(prices, item):
...
First word
Write first_word(sentence) that returns the first word of a sentence.
Words are separated by spaces, the sentence has at least one word, and
there may be extra spaces before the first word.
first_word("fresh bread daily") -> "fresh"
first_word(" apple pie") -> "apple"
def first_word(sentence):
...
Total stock
stock maps item names to quantities. Write total_stock(stock) that
returns the sum of all the quantities.
total_stock({"apple": 10, "bread": 3}) -> 13
total_stock({}) -> 0
def total_stock(stock):
...
Top of the stack
Write push_and_peek(stack, item) that pushes item onto the stack (a
list) and returns whatever is now on top.
push_and_peek(["a", "b"], "c") -> "c"
def push_and_peek(stack, item):
...
Leftover eggs
Eggs are packed in boxes of box_size. Write leftover(eggs, box_size)
that returns how many eggs are left after packing as many full boxes as possible.
leftover(20, 6) -> 2
leftover(12, 6) -> 0
def leftover(eggs, box_size):
...
Next: Warm-up 2.