How much do you know the basics of Python?

problem

Have your year entered as a Prameter --Return "drink toddy" if you are under 14 years old --Return "drink coke" if you are under 18 years old --Return "drink beer" if you are under 21 --Return "drink whiskey" if you are 21 years old or older Let's write the people_with_age_drink (age) function.

However, you must pass the following Unit Test.

people_with_age_drink(13) == "drink toddy"
people_with_age_drink(17) == "drink coke"
people_with_age_drink(18) == "drink beer"
people_with_age_drink(20) == "drink beer"
people_with_age_drink(30) == "drink whisky"

answer

"There's more than one way to do it" isn't just Perl. All the following answers are correct

def people_with_age_drink(age):
    if age<14 :
        return "drink toddy"
    elif age >= 14 and age < 18 :
        return "drink coke"
    elif age >= 18 and age < 21 :
        return "drink beer"
    else:
        return 'drink whisky'
def people_with_age_drink(age):
    if age < 14:
        return 'drink toddy'
    elif age >= 14 and age < 18:
        return 'drink coke'
    elif age >= 18 and age < 21:
        return 'drink beer'
    else:
        return 'drink whisky'
    return 'ERROR....WRONG INPUT'
def people_with_age_drink(age):
    return 'drink ' + next(d for a, d in [
        (21, 'whisky'), (18, 'beer'), (14, 'coke'), (0, 'toddy')
        ] if age >= a)
def people_with_age_drink(age):
    if 14 > age >= 0:
        return "drink toddy"
    if age < 18:
        return "drink coke"
    if age < 21:
        return "drink beer"
    return "drink whisky"    
def people_with_age_drink(age):
    if age<=13:    
        drink="toddy"
    elif age> 13 and age<=17:
        drink="coke"
    elif age<=20:
        drink="beer"
    else:   
        drink="whisky"
    return "drink " + drink
def people_with_age_drink(age):
    if (age < 14):
        return "drink toddy"
    if (age < 18):
        return "drink coke"
    if (age < 21):
        return "drink beer"
    if (age > 20):
        return "drink whisky"
def people_with_age_drink(age):
    if age > 20: return 'drink whisky'
    if age > 17: return 'drink beer'
    if age > 13: return 'drink coke'
    return 'drink toddy'
def people_with_age_drink(age):
    for a in [[21,'whisky'],[18,'beer'],[14,'coke'],[0,'toddy']]:
        if age >= a[0]:
            return "drink {0}".format(a[1])
def people_with_age_drink(age):
    table = { 13: "drink toddy", 17: "drink coke", 20: "drink beer", 200: "drink whisky" }
    vec = sorted(list(table.keys()))
    for key in vec:
        if age <= key:
            return table[key]
def people_with_age_drink(age):
    if age < 14: this = 'toddy'
    elif age < 18: this = 'coke'
    elif age < 21: this = 'beer'
    else: this = 'whisky'
    return 'drink {}'.format(this)
 def people_with_age_drink(age):
    drink = "toddy" if age < 14 else "coke" if age < 18 else "beer" if age < 21 else "whisky"
    return "drink " + drink
def people_with_age_drink(age):
    return "drink toddy" if age<14 else "drink coke" if age<18 else "drink beer" if age<21 else "drink whisky"
age_with_drink = {0: 'toddy', 14: 'coke', 18: 'beer', 21: 'whisky'}
def people_with_age_drink(age):
    return 'drink ' + next(age_with_drink[a] for a in sorted(age_with_drink, reverse=True) if age >= a)
def people_with_age_drink(age):
    bounds = [14,18,21,age+1]
    drinks = ["toddy","coke","beer","whisky"]
    for x in range(len(bounds)): 
        if age < bounds[x]: return "drink " + drinks[x]
drinks = [
    ("whisky", 21),
    ("beer", 18),
    ("coke", 14),
]
def people_with_age_drink(age):
    return "drink " + next((drink for drink, age_limit in drinks if age >= age_limit), "toddy")
def people_with_age_drink(age):
    for (lim, drink) in [(14,"toddy"), (18, "coke"), (21, "beer"), (float('inf'), "whisky")]:
        if age < lim:
            return "drink " + drink
def people_with_age_drink(age):
    res = "drink whisky"
    drinks = (
        (14, "drink toddy"),
        (18, "drink coke"),
        (21, "drink beer"),
    )
    for a in drinks:
        if age < a[0]:
            res = a[1]
            break
    return res
def people_with_age_drink(age):
    return 'drink ' + ('toddy' if age < 14 else 'coke' if age < 18 else 'beer' if age < 21 else 'whisky')
def people_with_age_drink(age):
   return 'drink '+['toddy', 'coke', 'beer', 'whisky'][(age/3>5)+(age/7>1)+(age/7>2)]
def people_with_age_drink(age):
    predMessage = [(age < 14, "toddy"), (age < 18,"coke"),
                   (age < 21, "beer"), (age >= 21, "whisky")]
    for filt,message in predMessage:
        if filt:
            return "drink " + message
def people_with_age_drink(age):
    return "drink {}".format("toddy" if age < 14 else
                             "coke"  if age < 18 else
                             "beer"  if age < 21 else
                             "whisky")
import sys
def people_with_age_drink(age):
    drink = (( 0, 13, 'toddy'),
             (14, 17, 'coke'),
             (18, 20, 'beer'),
             (21, sys.maxint, 'whisky'))
    return 'drink ' + filter(lambda d: d[0] <= age <= d[1], drink)[0][2]
people_with_age_drink = lambda old: "drink " + ((("whisky", "beer")[old < 21], "coke")[old < 18], "toddy")[old < 14]
people_with_age_drink = lambda age: 'drink ' + ('toddy' if age < 14 else 
                                                'coke'  if age < 18 else
                                                'beer'  if age < 21 else
                                                'whisky')

There are likely to be many answers to the FizzBuzz question. It's often said that Python is "similar code no matter who writes it," but I don't think so.

Recommended Posts

How much do you know the basics of Python?
I didn't know the basics of Python
Review of the basics of Python (FizzBuzz)
About the basics list of Python basics
Learn the basics of Python ① Beginners
[Python3] Understand the basics of Beautiful Soup
How to know the internal structure of an object in Python
Explaining the mechanism of Linux that you do not know unexpectedly
Basics of Python ①
Basics of python ①
The basics of running NoxPlayer in Python
[Python3] Understand the basics of file operations
How many types of Python do you have in Windows 10? I had 5 types.
Python Note: When you want to know the attributes of an object
Basics of Python scraping basics
the zen of Python
# 4 [python] Basics of functions
Basics of python: Output
You will be an engineer in 100 days --Day 29 --Python --Basics of the Python language 5
You will be an engineer in 100 days --Day 33 --Python --Basics of the Python language 8
You will be an engineer in 100 days --Day 26 --Python --Basics of the Python language 3
How many types of Python do you have on your macOS? I had 401 types.
You will be an engineer in 100 days --Day 32 --Python --Basics of the Python language 7
You will be an engineer in 100 days --Day 28 --Python --Basics of the Python language 4
How to know the port number of the xinetd service
How to get the number of digits in Python
How to know the current directory in Python in Blender
Let's break down the basics of TensorFlow Python code
How to use Python Kivy ① ~ Basics of Kv Language ~
Which day of the week do you buy gold?
[Python] Summary of how to specify the color of the figure
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
[Python] How do you use lambda expressions? ?? [Scribbles] [Continued-1]
Python Minor Environment Retsuden-How many Python environments do you know? ~
EP 1 Know Which Version of Python You ’re Using.
[Introduction to Python] Basic usage of the library scipy that you absolutely must know
Towards the retirement of Python2
About the ease of Python
python: Basics of using scikit-learn ①
About the features of Python
How do you collect information?
Basics of Python × GIS (Part 1)
The Power of Pandas: Python
The story of how the Python bottle worked on Sakura Internet
What you can do with the Python standard library statistics
I didn't know how to use the [python] for statement
The websocket of toio (nodejs) and python / websocket do not connect.
I want to know the features of Python and pip
What beginners learned from the basics of variables in python
[Python] What do you do with visualization of 4 or more variables?
Basics of Python x GIS (Part 3)
Paiza Python Primer 5: Basics of Dictionaries
[Python] How to do PCA in Python
The story of Python and the story of NaN
How to determine the existence of a selenium element in Python
[Python] How to force a method of a subclass to do something specific
How to change the log level of Azure SDK for Python
[Python] The stumbling block of import
First Python 3 ~ The beginning of repetition ~
Do you need a Python re.compile?
How to check the memory size of a variable in Python