3.5 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 69e2383af7832c8032603b94 | Challenge 278: Coffee Order Parser | 29 | challenge-278 |
--description--
Given a string for a coffee order, identify any menu items and return a formatted order.
Use the following menu items and prices:
| Item | Price |
|---|---|
"cold brew" |
$4.50 |
"oat latte" |
$5.00 |
"cappuccino" |
$4.75 |
"espresso" |
$3.00 |
"vanilla syrup" |
$0.75 |
"caramel drizzle" |
$0.60 |
"extra shot" |
$0.50 |
"oat milk" |
$0.75 |
"cream" |
$0.75 |
Return a string with the matched items joined by " + ", followed by a colon and space (": "), and the total price.
For example, given "I'd like an oat latte with vanilla syrup and an extra shot please.", return "oat latte + vanilla syrup + extra shot: $6.25"
Items should appear in the order they appear in the menu and the total price should always have two decimal places.
--hints--
format_coffee_order("I'd like an oat latte with vanilla syrup and an extra shot please.") should return "oat latte + vanilla syrup + extra shot: $6.25".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(format_coffee_order("I'd like an oat latte with vanilla syrup and an extra shot please."), "oat latte + vanilla syrup + extra shot: $6.25")`)
}})
format_coffee_order("Give me a cappuccino with caramel drizzle, vanilla syrup, and some oat milk.") should return "cappuccino + vanilla syrup + caramel drizzle + oat milk: $6.85".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(format_coffee_order("Give me a cappuccino with caramel drizzle, vanilla syrup, and some oat milk."), "cappuccino + vanilla syrup + caramel drizzle + oat milk: $6.85")`)
}})
format_coffee_order("Can I get a cold brew with some cream and an extra shot.") should return "cold brew + extra shot + cream: $5.75".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(format_coffee_order("Can I get a cold brew with some cream and an extra shot."), "cold brew + extra shot + cream: $5.75")`)
}})
format_coffee_order("Just an espresso please.") should return "espresso: $3.00".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(format_coffee_order("Just an espresso please."), "espresso: $3.00")`)
}})
format_coffee_order("I'll take an oat latte with cream and an extra shot, and some vanilla syrup and caramel drizzle.") should return "oat latte + vanilla syrup + caramel drizzle + extra shot + cream: $7.60".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(format_coffee_order("I'll take an oat latte with cream and an extra shot, and some vanilla syrup and caramel drizzle."), "oat latte + vanilla syrup + caramel drizzle + extra shot + cream: $7.60")`)
}})
--seed--
--seed-contents--
def format_coffee_order(order):
return order
--solutions--
def format_coffee_order(order):
menu = [
("cold brew", 450),
("oat latte", 500),
("cappuccino", 475),
("espresso", 300),
("vanilla syrup", 75),
("caramel drizzle", 60),
("extra shot", 50),
("oat milk", 75),
("cream", 75),
]
found = [(name, price) for name, price in menu if name in order.lower()]
total = sum(price for _, price in found)
items = " + ".join(name for name, _ in found)
return f"{items}: ${total / 100:.2f}"