Решение на Да си приготвим нещо за хапване от Димитър Сакъров

Обратно към всички решения

Към профила на Димитър Сакъров

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 10 успешни тест(а)
  • 0 неуспешни тест(а)

Код

def prepare_meal(number):
"""
Function that cooks spam and eggs based on the quantity required.
More details can be found here:
https://github.com/fmi/python-homework/tree/master/2012/01
"""
# Count the spam
spam_count = 0
while (number % 3 == 0):
spam_count += 1
number = number / 3
if (number % 5 == 0): # Add eggs
result = "eggs"
if (spam_count > 0):
ingredients = ["spam and ", "spam "]
result = cook(spam_count, ingredients) + result
elif (spam_count > 0): # Only spam
ingredients = ["spam", "spam "]
result = cook(spam_count, ingredients)
else:
result = ""
return result
def cook(spam_count, ingredients):
result = ""
for i in range(0, spam_count):
if (i == 0):
result = ingredients[0] + result
else:
result = ingredients[1] + result
return result

Лог от изпълнението

..........
----------------------------------------------------------------------
Ran 10 tests in 0.001s

OK

История (1 версия и 0 коментара)

Димитър обнови решението на 09.03.2012 19:22 (преди над 8 години)

+def prepare_meal(number):
+ """
+ Function that cooks spam and eggs based on the quantity required.
+
+ More details can be found here:
+ https://github.com/fmi/python-homework/tree/master/2012/01
+ """
+
+ # Count the spam
+ spam_count = 0
+ while (number % 3 == 0):
+ spam_count += 1
+ number = number / 3
+
+ if (number % 5 == 0): # Add eggs
+ result = "eggs"
+ if (spam_count > 0):
+ ingredients = ["spam and ", "spam "]
+ result = cook(spam_count, ingredients) + result
+ elif (spam_count > 0): # Only spam
+ ingredients = ["spam", "spam "]
+ result = cook(spam_count, ingredients)
+ else:
+ result = ""
+ return result
+
+
+def cook(spam_count, ingredients):
+ result = ""
+ for i in range(0, spam_count):
+ if (i == 0):
+ result = ingredients[0] + result
+ else:
+ result = ingredients[1] + result
+ return result