DEV Community

Discussion on: Daily Challenge #241 - Tip Calculator

Collapse
 
agtoever profile image
agtoever

Python 3

# Solution
import math
RATINGS = {
    'terrible': .0,
    'poor': 0.05,
    'good': 0.1,
    'great': 0.15,
    'excellent': 0.2
}

tip = lambda p, s:  math.ceil(p * RATINGS[s.lower()]) if s.lower() in RATINGS else 'Rating not recognized'

# Test cases
cases = [
    (30, "poor"),
    (20, "hi"),
    (107.65, "great"),
    (78, "good"),
    (50, "poor"),
    (125, "excellent"),
]

for case in cases:
    print(f'With score "{case[1]}" and amount {case[0]}, tip: {tip(*case)}')

Try it online!