Pythonの基本、どこまで知っていますか？

# 問題
お年をPrameterとして入力してもらって
 - 14才未満であれば "drink toddy" をreturn
 - 18才未満であれば "drink coke" をreturn
 - 21才未満であれば "drink beer" をreturn
 - 21才以上であれば "drink whisky" をreturn
する people_with_age_drink(age) 関数を書いてみましょう。

ただし、以下のUnitTestをPassしなければいけません。

```py
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"
```

# 答え
"There's more than one way to do it"はPerlだけじゃありません。
以下のすべての答えが正解
※もっとありそうならコメントをお願いします。

```py
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'
```

```py
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'
```

```py
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)
```

```py     
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"    
```

```py
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
```

```py
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"
```   
   
```py     
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'
```

```py
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])
```

```py
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]
```       

```py
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)
```

```py 
 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
```

```py
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"
```

```py
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)
```

```py
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]
```

```py
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")
```

```py
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
```       

```py
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
```
   
```py 
def people_with_age_drink(age):
    return 'drink ' + ('toddy' if age < 14 else 'coke' if age < 18 else 'beer' if age < 21 else 'whisky')
```

```py
def people_with_age_drink(age):
   return 'drink '+['toddy', 'coke', 'beer', 'whisky'][(age/3>5)+(age/7>1)+(age/7>2)]
```

```py
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
```       
   
```py         
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")
```

```py                        
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]
```    

```py 
people_with_age_drink = lambda old: "drink " + ((("whisky", "beer")[old < 21], "coke")[old < 18], "toddy")[old < 14]
```

```py
people_with_age_drink = lambda age: 'drink ' + ('toddy' if age < 14 else 
                                                'coke'  if age < 18 else
                                                'beer'  if age < 21 else
                                                'whisky')
```

FizzBuzz問題の答えもたくさんありそうですね。
Pythonは「誰が書いても同じようなコードになる」とよく紹介されていますが、そうでもない気がします。