Lassen Sie Ihr Jahr als Prameter eingeben
Sie müssen jedoch den folgenden Komponententest bestehen.
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"
"Es gibt mehr als einen Weg, dies zu tun" ist nicht nur für Perl. Alle folgenden Antworten sind richtig
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')
Es scheint viele Antworten auf das FizzBuzz-Problem zu geben. Es wird oft gesagt, dass Python "ähnlicher Code ist, egal wer ihn schreibt", aber ich denke nicht.