반응형
Complete the function, which calculates how much you need to tip based on the total amount of the bill and the service.
You need to consider the following ratings:
- Terrible: tip 0%
- Poor: tip 5%
- Good: tip 10%
- Great: tip 15%
- Excellent: tip 20%
The rating is case insensitive (so "great" = "GREAT"). If an unrecognised rating is received, then you need to return:
- "Rating not recognised" in Javascript, Python and Ruby...
- ...or null in Java
- ...or -1 in C#
Because you're a nice person, you always round up the tip, regardless of the service.
Solution:
tip_table = {
"terrible": 0,
"poor": 0.05,
"good": 0.1,
"great": 0.15,
"excellent": 0.2,
}
def calculate_tip(amount: float, rating: str, tip_table: dict[str, float] = tip_table) -> str | int:
tip_rate = tip_table.get(rating.lower())
if tip_rate is None:
return 'Rating not recognised'
import math
return math.ceil(amount * tip_rate)
반응형
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
0114. Are arrow functions odd? (0) | 2023.01.15 |
---|---|
0113. Build a square (0) | 2023.01.13 |
0111. You Can't Code Under Pressure #1 (0) | 2023.01.12 |
0110. altERnaTIng cAsE <=> ALTerNAtiNG CaSe (0) | 2023.01.11 |
0109. A Needle in the Haystack (0) | 2023.01.09 |