본문 바로가기
나는 이렇게 학습한다/Algorithm & SQL

0112. Tip Calculator

by daco2020 2023. 1. 13.

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)