Решение на Хороскоп от Йордан Джамбазов

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

Към профила на Йордан Джамбазов

Резултати

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

Код

"""Solution for Task 1. The module contains a function that calculates
the horoscope sign by the date of birth."""
def what_is_my_sign(day, month):
"""Retrieve the name of the horoscope sign by day of birth.
Keyword arguments:
day -- the day of birth
month -- the month of birth
Returns string with the name of the horoscope sign.
"""
# List containing information about every sign. The first column
# contains the name of the sign, second and third - the start date,
# columns four and five - the final date.
signs = [
# Name Start End
['Овен', (21, 3), (20, 4)],
['Телец', (21, 4), (20, 5)],
['Близнаци', (21, 5), (20, 6)],
['Рак', (21, 6), (21, 7)],
['Лъв', (22, 7), (22, 8)],
['Дева', (23, 8), (22, 9)],
['Везни', (23, 9), (22, 10)],
['Скорпион', (23, 10), (21, 11)],
['Стрелец', (22, 11), (21, 12)],
['Козирог', (22, 12), (19, 1)],
['Водолей', (20, 1), (18, 2)],
['Риби', (19, 2), (20, 3)],
]
# Iterate through all the horoscope signs and try to find a match.
for sign in signs:
start_day = sign[1][0]
start_month = sign[1][1]
end_day = sign[2][0]
end_month = sign[2][1]
# Return the sign name if the given date is in the interval.
if day >= start_day and month == start_month or \
day <= end_day and month == end_month:
return sign[0]

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

...............
----------------------------------------------------------------------
Ran 15 tests in 0.002s

OK

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

Йордан обнови решението на 28.02.2013 18:01 (преди над 11 години)

+# -*- coding: utf-8 -*-
+"""Solution for Task 1. The module contains a function that calculates
+the horoscope sign by the date of birth."""
+
+
+def what_is_my_sign(day, month):
+ """Retrieve the name of the horoscope sign by day of birth.
+
+ Keyword arguments:
+ day -- the day of birth
+ month -- the month of birth
+
+ Returns string with the name of the horoscope sign.
+ """
+
+ # List containing information about every sign. The first column
+ # contains the name of the sign, second and third - the start date,
+ # columns four and five - the final date.
+ signs = [
+ # Name Start End
+ ['Овен', 21, 3, 20, 4],
+ ['Телец', 21, 4, 20, 5],
+ ['Близнаци', 21, 5, 20, 6],
+ ['Рак', 21, 6, 21, 7],
+ ['Лъв', 22, 7, 22, 8],
+ ['Дева', 23, 8, 22, 9],
+ ['Везни', 23, 9, 22, 10],
+ ['Скорпион', 23, 10, 21, 11],
+ ['Стрелец', 22, 11, 21, 12],
+ ['Козирог', 22, 12, 19, 1],
+ ['Водолей', 20, 1, 18, 2],
+ ['Рак', 19, 2, 20, 3],
+ ]
+
+ # Iterate through all the horoscope signs and try to find a match.
+ for sign in signs:
+ start_day = sign[1]
+ start_month = sign[2]
+ end_day = sign[3]
+ end_month = sign[4]
+
+ # Return the sign name if the given date is in the interval.
+ if day >= start_day and month == start_month or \
+ day <= end_day and month == end_month:
+ name = sign[0]
+ return name

Добро решение. Само няколко дребни забележки:

  • Ако пишеш на python 3.x това не ти трябва: # -*- coding: utf-8 -*-
  • Яко е, че знаеш за docstring-овете и те са OK. Коментарите обаче из кода са излишни. Пишем на език, който е доста експресивен и няма нужда от това изрично да казваме какво прави нашият код.
  • Аз бих направил списъкът с три елемента. Двете двойки месец и ден бих ги поставил в tuple.
  • Няма голям смисъл първо да assing-ваш sign[0] на name и чак тогава да го връщаш. return sign[0] е напълно достатъчно.

Като изключим тези дребни детайли ми харесва решението ти. Очевидно си се постарал да избереш добри имена, което прави кодът ти доволно четим. +1 точка от мен за това.

Мерси за забележките (и за бонус точката).

  • coding: utf-8 ми е стара травма от Python 2.7 :)
  • Добрият код се самодокументира. :)
  • Идеята за tuple-а ми харесва.
  • Преди да направя return на name, съм го сложил в отделна променлива за да е ясно какво точно се връща. Може би малко съм го "предобрил" кода.

А каква е процедурата за получаване на останалите точки?

Йордан обнови решението на 01.03.2013 23:53 (преди над 11 години)

# -*- coding: utf-8 -*-
"""Solution for Task 1. The module contains a function that calculates
the horoscope sign by the date of birth."""
def what_is_my_sign(day, month):
"""Retrieve the name of the horoscope sign by day of birth.
Keyword arguments:
day -- the day of birth
month -- the month of birth
Returns string with the name of the horoscope sign.
"""
# List containing information about every sign. The first column
# contains the name of the sign, second and third - the start date,
# columns four and five - the final date.
signs = [
# Name Start End
['Овен', 21, 3, 20, 4],
['Телец', 21, 4, 20, 5],
['Близнаци', 21, 5, 20, 6],
['Рак', 21, 6, 21, 7],
['Лъв', 22, 7, 22, 8],
['Дева', 23, 8, 22, 9],
['Везни', 23, 9, 22, 10],
['Скорпион', 23, 10, 21, 11],
['Стрелец', 22, 11, 21, 12],
['Козирог', 22, 12, 19, 1],
['Водолей', 20, 1, 18, 2],
- ['Рак', 19, 2, 20, 3],
+ ['Риби', 19, 2, 20, 3],
]
# Iterate through all the horoscope signs and try to find a match.
for sign in signs:
start_day = sign[1]
start_month = sign[2]
end_day = sign[3]
end_month = sign[4]
# Return the sign name if the given date is in the interval.
if day >= start_day and month == start_month or \
day <= end_day and month == end_month:
name = sign[0]
return name

Йордан обнови решението на 01.03.2013 23:57 (преди над 11 години)

-# -*- coding: utf-8 -*-
"""Solution for Task 1. The module contains a function that calculates
the horoscope sign by the date of birth."""
def what_is_my_sign(day, month):
"""Retrieve the name of the horoscope sign by day of birth.
Keyword arguments:
day -- the day of birth
month -- the month of birth
Returns string with the name of the horoscope sign.
"""
# List containing information about every sign. The first column
# contains the name of the sign, second and third - the start date,
# columns four and five - the final date.
signs = [
# Name Start End
- ['Овен', 21, 3, 20, 4],
- ['Телец', 21, 4, 20, 5],
- ['Близнаци', 21, 5, 20, 6],
- ['Рак', 21, 6, 21, 7],
- ['Лъв', 22, 7, 22, 8],
- ['Дева', 23, 8, 22, 9],
- ['Везни', 23, 9, 22, 10],
- ['Скорпион', 23, 10, 21, 11],
- ['Стрелец', 22, 11, 21, 12],
- ['Козирог', 22, 12, 19, 1],
- ['Водолей', 20, 1, 18, 2],
- ['Риби', 19, 2, 20, 3],
+ ['Овен', (21, 3), (20, 4)],
+ ['Телец', (21, 4), (20, 5)],
+ ['Близнаци', (21, 5), (20, 6)],
+ ['Рак', (21, 6), (21, 7)],
+ ['Лъв', (22, 7), (22, 8)],
+ ['Дева', (23, 8), (22, 9)],
+ ['Везни', (23, 9), (22, 10)],
+ ['Скорпион', (23, 10), (21, 11)],
+ ['Стрелец', (22, 11), (21, 12)],
+ ['Козирог', (22, 12), (19, 1)],
+ ['Водолей', (20, 1), (18, 2)],
+ ['Риби', (19, 2), (20, 3)],
]
# Iterate through all the horoscope signs and try to find a match.
for sign in signs:
- start_day = sign[1]
- start_month = sign[2]
- end_day = sign[3]
- end_month = sign[4]
+ start_day = sign[1][0]
+ start_month = sign[1][1]
+ end_day = sign[2][0]
+ end_month = sign[2][1]
# Return the sign name if the given date is in the interval.
if day >= start_day and month == start_month or \
day <= end_day and month == end_month:
- name = sign[0]
- return name
+ return sign[0]
+

Йордан обнови решението на 02.03.2013 21:40 (преди над 11 години)

"""Solution for Task 1. The module contains a function that calculates
the horoscope sign by the date of birth."""
def what_is_my_sign(day, month):
"""Retrieve the name of the horoscope sign by day of birth.
-
+
Keyword arguments:
day -- the day of birth
month -- the month of birth
-
+
Returns string with the name of the horoscope sign.
"""
# List containing information about every sign. The first column
# contains the name of the sign, second and third - the start date,
# columns four and five - the final date.
signs = [
- # Name Start End
+ # Name Start End
['Овен', (21, 3), (20, 4)],
['Телец', (21, 4), (20, 5)],
['Близнаци', (21, 5), (20, 6)],
['Рак', (21, 6), (21, 7)],
['Лъв', (22, 7), (22, 8)],
['Дева', (23, 8), (22, 9)],
['Везни', (23, 9), (22, 10)],
['Скорпион', (23, 10), (21, 11)],
['Стрелец', (22, 11), (21, 12)],
['Козирог', (22, 12), (19, 1)],
['Водолей', (20, 1), (18, 2)],
['Риби', (19, 2), (20, 3)],
]
# Iterate through all the horoscope signs and try to find a match.
for sign in signs:
start_day = sign[1][0]
start_month = sign[1][1]
end_day = sign[2][0]
end_month = sign[2][1]
-
+
# Return the sign name if the given date is in the interval.
if day >= start_day and month == start_month or \
day <= end_day and month == end_month:
return sign[0]
-