반응형
Complete the function that receives as input a string, and produces outputs according to the following table:
Input | Output |
---|---|
"Jabroni" | "Patron Tequila" |
"School Counselor" | "Anything with Alcohol" |
"Programmer" | "Hipster Craft Beer" |
"Bike Gang Member" | "Moonshine" |
"Politician" | "Your tax dollars" |
"Rapper" | "Cristal" |
anything else | "Beer" |
Note: anything else is the default case: if the input to the function is not any of the values in the table, then the return value should be "Beer".
Make sure you cover the cases where certain words do not show up with correct capitalization. For example, the input "pOLitiCIaN" should still return "Your tax dollars".
Solution:
from dataclasses import dataclass
@dataclass
class UserData:
id: int
profession: str
@dataclass
class RequestData:
user_id: int
drink: str
USER_DATA = [
UserData(id=1, profession="Jabroni"),
UserData(id=2, profession="School Counselor"),
UserData(id=3, profession="Programmer"),
UserData(id=4, profession="Bike Gang Member"),
UserData(id=5, profession="Politician"),
UserData(id=6, profession="Rapper"),
]
REQUEST_DATA = [
RequestData(user_id=1, drink="Patron Tequila"),
RequestData(user_id=2, drink="Anything with Alcohol"),
RequestData(user_id=3, drink="Hipster Craft Beer"),
RequestData(user_id=4, drink="Moonshine"),
RequestData(user_id=5, drink="Your tax dollars"),
RequestData(user_id=6, drink="Cristal"),
]
def get_user_id(func):
def wrapper(profession: str):
user_id = 0
for user in USER_DATA:
if user.profession.casefold() == profession.casefold():
user_id = user.id
return func(user_id)
return wrapper
@get_user_id
def get_drink_by_profession(user_id: int) -> str:
drink = 'Beer'
for request in REQUEST_DATA:
if request.user_id == user_id:
drink = request.drink
return drink
반응형
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
0905. Training JS #7: if..else and ternary operator (0) | 2022.09.05 |
---|---|
0904. No zeros for heros (0) | 2022.09.04 |
0902. Simple multiplication (0) | 2022.09.02 |
0901. Alan Partridge II - Apple Turnover (0) | 2022.09.01 |
0831. Correct the mistakes of the character recognition software (0) | 2022.08.31 |