Решение на Да си приготвим нещо за хапване от Лъчезар Цанов

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

Към профила на Лъчезар Цанов

Резултати

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

Код

import math
def make_food(number):
top = int(math.log(number, 3) + 1)
the_food = ['spam ' for x in range(1, top) if number % pow(3, x) == 0]
if (number % 5 == 0 and the_food == []):
return 'eggs'
elif(number % 5 == 0):
the_food.append('and eggs')
return ''.join(the_food).strip()

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

EEEEEEEEEE
======================================================================
ERROR: test_eggs (__main__.FirstHomeworkTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120506-8116-tqupws", line 19, in test_eggs
    self.assertEqual('eggs', self.solution.prepare_meal(5))
AttributeError: 'module' object has no attribute 'prepare_meal'

======================================================================
ERROR: test_eggs_with_additional_egg_multipliers (__main__.FirstHomeworkTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120506-8116-tqupws", line 22, in test_eggs_with_additional_egg_multipliers
    self.assertEqual('eggs', self.solution.prepare_meal(5**4))
AttributeError: 'module' object has no attribute 'prepare_meal'

======================================================================
ERROR: test_eggs_with_additional_nonegg_multipliers (__main__.FirstHomeworkTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120506-8116-tqupws", line 25, in test_eggs_with_additional_nonegg_multipliers
    self.assertEqual('eggs', self.solution.prepare_meal(5*7*11))
AttributeError: 'module' object has no attribute 'prepare_meal'

======================================================================
ERROR: test_no_spam_or_eggs (__main__.FirstHomeworkTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120506-8116-tqupws", line 7, in test_no_spam_or_eggs
    self.assertEqual('', self.solution.prepare_meal(11))
AttributeError: 'module' object has no attribute 'prepare_meal'

======================================================================
ERROR: test_one_spam (__main__.FirstHomeworkTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120506-8116-tqupws", line 10, in test_one_spam
    self.assertEqual('spam', self.solution.prepare_meal(3))
AttributeError: 'module' object has no attribute 'prepare_meal'

======================================================================
ERROR: test_some_spam_and_eggs (__main__.FirstHomeworkTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120506-8116-tqupws", line 31, in test_some_spam_and_eggs
    self.assertEqual('spam spam and eggs', self.solution.prepare_meal(45))
AttributeError: 'module' object has no attribute 'prepare_meal'

======================================================================
ERROR: test_some_spams (__main__.FirstHomeworkTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120506-8116-tqupws", line 13, in test_some_spams
    self.assertEqual('spam spam spam spam', self.solution.prepare_meal(81))
AttributeError: 'module' object has no attribute 'prepare_meal'

======================================================================
ERROR: test_some_spams_with_additional_multipliers (__main__.FirstHomeworkTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120506-8116-tqupws", line 16, in test_some_spams_with_additional_multipliers
    self.assertEqual('spam spam spam spam', self.solution.prepare_meal(81*2*7*11))
AttributeError: 'module' object has no attribute 'prepare_meal'

======================================================================
ERROR: test_spam_and_eggs (__main__.FirstHomeworkTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120506-8116-tqupws", line 28, in test_spam_and_eggs
    self.assertEqual('spam and eggs', self.solution.prepare_meal(15))
AttributeError: 'module' object has no attribute 'prepare_meal'

======================================================================
ERROR: test_spam_and_eggs_with_additional_multipliers (__main__.FirstHomeworkTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120506-8116-tqupws", line 34, in test_spam_and_eggs_with_additional_multipliers
    self.assertEqual('spam spam and eggs', self.solution.prepare_meal(45 * 8 * 7))
AttributeError: 'module' object has no attribute 'prepare_meal'

----------------------------------------------------------------------
Ran 10 tests in 0.002s

FAILED (errors=10)

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

Лъчезар обнови решението на 08.03.2012 17:39 (преди над 8 години)

+import math
+def make_food(number):
+ top = int(math.log(number, 3) + 1)
+ the_food = ['spam ' for x in range(1, top) if number % pow(3, x) == 0]
+ if (number % 5 == 0 and the_food == []):
+ return 'eggs'
+ elif(number % 5 == 0):
+ the_food.append('and eggs')
+ return ''.join(the_food).strip()

докато писах моето домашно попаднах на едно-две форумни твърдения, че да закръгляш с конструктор за int не е много добра идея, т.к. целта му не е да закръгли, а да направи инстанция и формално нямаш представа каква магия би правила на аргумента. Може би е по-коректно да използваш round/trunc в тази ситуация.

не е лошо да имаш 2 реда между import math и def make_food...(което трябваше да е prepare_meal)

и накрая няма нужда да правиш това:

''.join(the_food).strip()

резултата от:

the_food.strip()

е абсолютно същия

ох малко тъпо не усетих че съм променил името на функцията и сега няма да мина нито 1 тест :(

Иначе the_food e лист от стрингове и не мога да му приложа директно strip() първо трябва да го слея в стринг