Решение на Астрологични забави от Явор Михайлов

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

Към профила на Явор Михайлов

Резултати

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

Код

def interpret_western_sign(day, month):
zodiacal_signs_data = [
('aquarius', 21, 1),
('pisces', 19, 2),
('aries', 21, 3),
('taurus', 21, 4),
('gemini', 21, 5),
('cancer', 21, 6),
('leo', 23, 7),
('virgo', 23, 8),
('libra', 23, 9),
('scorpio', 23, 10),
('sagittarius', 22, 11),
('capricorn', 22, 12),
]
for i in range(len(zodiacal_signs_data)):
month_number = i + 1
if month == month_number:
if day < zodiacal_signs_data[i][1]:
return(zodiacal_signs_data[i - 1][0])
else:
return(zodiacal_signs_data[i][0])
def interpret_chinese_sign(year):
chinese_zodiac_signs = [
'monkey', 'rooster', 'dog', 'pig',
'rat', 'ox', 'tiger', 'rabbit', 'dragon',
'snake', 'horse', 'sheep',
]
return chinese_zodiac_signs[year % len(chinese_zodiac_signs)]
def interpret_both_signs(day, month, year):
return interpret_western_sign(day, month), interpret_chinese_sign(year)

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

......
----------------------------------------------------------------------
Ran 6 tests in 0.006s

OK

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

Явор обнови решението на 06.03.2015 00:32 (преди над 9 години)

+import datetime
+
+
+def interpret_western_sign(day, month):
+
+ zodiacal_signs_data = [
+ {
+ 'sign_name': 'aquarius',
+ 'start_date': datetime.date(2015, 1, 21),
+ 'end_date': datetime.date(2015, 2, 19),
+ },
+ {
+ 'sign_name': 'pisces',
+ 'start_date': datetime.date(2015, 2, 19),
+ 'end_date': datetime.date(2015, 3, 20),
+ },
+ {
+ 'sign_name': 'capricorn',
+ 'start_date': datetime.date(2015, 12, 22),
+ 'end_date': datetime.date(2016, 1, 20),
+ },
+ {
+ 'sign_name': 'sagittarius',
+ 'start_date': datetime.date(2015, 11, 22),
+ 'end_date': datetime.date(2015, 12, 21),
+ },
+ {
+ 'sign_name': 'scorpio',
+ 'start_date': datetime.date(2015, 10, 23),
+ 'end_date': datetime.date(2015, 11, 21),
+ },
+ {
+ 'sign_name': 'libra',
+ 'start_date': datetime.date(2015, 9, 23),
+ 'end_date': datetime.date(2015, 10, 22),
+ },
+ {
+ 'sign_name': 'virgo',
+ 'start_date': datetime.date(2015, 8, 23),
+ 'end_date': datetime.date(2015, 9, 22),
+ },
+ {
+ 'sign_name': 'leo',
+ 'start_date': datetime.date(2015, 7, 23),
+ 'end_date': datetime.date(2015, 8, 22),
+ },
+ {
+ 'sign_name': 'cancer',
+ 'start_date': datetime.date(2015, 6, 21),
+ 'end_date': datetime.date(2015, 7, 22),
+ },
+ {
+ 'sign_name': 'gemini',
+ 'start_date': datetime.date(2015, 5, 21),
+ 'end_date': datetime.date(2015, 6, 20),
+ },
+ {
+ 'sign_name': 'aries',
+ 'start_date': datetime.date(2015, 3, 21),
+ 'end_date': datetime.date(2015, 4, 20),
+ },
+ {
+ 'sign_name': 'taurus',
+ 'start_date': datetime.date(2015, 4, 21),
+ 'end_date': datetime.date(2015, 5, 20),
+ },
+ ]
+
+ target_date = datetime.date(2015, month, day)
+
+ for sign in zodiacal_signs_data:
+ if sign['start_date'] <= target_date <= sign['end_date']:
+ return sign['sign_name']
+
+
+def interpret_chinese_sign(year):
+
+ chinese_zodiac_signs = [
+ 'monkey', 'rooster', 'dog', 'pig',
+ 'rat', 'ox', 'tiger', 'rabbit', 'dragon',
+ 'snake', 'horse', 'sheep',
+ ]
+
+ return chinese_zodiac_signs[year % 12]
+
+
+def interpret_both_signs(day, month, year):
+ return(interpret_western_sign(day, month), interpret_chinese_sign(year))

Явор обнови решението на 06.03.2015 00:33 (преди над 9 години)

import datetime
def interpret_western_sign(day, month):
-
zodiacal_signs_data = [
{
'sign_name': 'aquarius',
'start_date': datetime.date(2015, 1, 21),
'end_date': datetime.date(2015, 2, 19),
},
{
'sign_name': 'pisces',
'start_date': datetime.date(2015, 2, 19),
'end_date': datetime.date(2015, 3, 20),
},
{
'sign_name': 'capricorn',
'start_date': datetime.date(2015, 12, 22),
'end_date': datetime.date(2016, 1, 20),
},
{
'sign_name': 'sagittarius',
'start_date': datetime.date(2015, 11, 22),
'end_date': datetime.date(2015, 12, 21),
},
{
'sign_name': 'scorpio',
'start_date': datetime.date(2015, 10, 23),
'end_date': datetime.date(2015, 11, 21),
},
{
'sign_name': 'libra',
'start_date': datetime.date(2015, 9, 23),
'end_date': datetime.date(2015, 10, 22),
},
{
'sign_name': 'virgo',
'start_date': datetime.date(2015, 8, 23),
'end_date': datetime.date(2015, 9, 22),
},
{
'sign_name': 'leo',
'start_date': datetime.date(2015, 7, 23),
'end_date': datetime.date(2015, 8, 22),
},
{
'sign_name': 'cancer',
'start_date': datetime.date(2015, 6, 21),
'end_date': datetime.date(2015, 7, 22),
},
{
'sign_name': 'gemini',
'start_date': datetime.date(2015, 5, 21),
'end_date': datetime.date(2015, 6, 20),
},
{
'sign_name': 'aries',
'start_date': datetime.date(2015, 3, 21),
'end_date': datetime.date(2015, 4, 20),
},
{
'sign_name': 'taurus',
'start_date': datetime.date(2015, 4, 21),
'end_date': datetime.date(2015, 5, 20),
},
]
target_date = datetime.date(2015, month, day)
for sign in zodiacal_signs_data:
if sign['start_date'] <= target_date <= sign['end_date']:
return sign['sign_name']
def interpret_chinese_sign(year):
-
chinese_zodiac_signs = [
'monkey', 'rooster', 'dog', 'pig',
'rat', 'ox', 'tiger', 'rabbit', 'dragon',
'snake', 'horse', 'sheep',
]
return chinese_zodiac_signs[year % 12]
def interpret_both_signs(day, month, year):
- return(interpret_western_sign(day, month), interpret_chinese_sign(year))
+ return(interpret_western_sign(day, month), interpret_chinese_sign(year))

Явор обнови решението на 06.03.2015 00:35 (преди над 9 години)

import datetime
def interpret_western_sign(day, month):
zodiacal_signs_data = [
{
'sign_name': 'aquarius',
'start_date': datetime.date(2015, 1, 21),
'end_date': datetime.date(2015, 2, 19),
},
{
'sign_name': 'pisces',
'start_date': datetime.date(2015, 2, 19),
'end_date': datetime.date(2015, 3, 20),
},
{
'sign_name': 'capricorn',
'start_date': datetime.date(2015, 12, 22),
'end_date': datetime.date(2016, 1, 20),
},
{
'sign_name': 'sagittarius',
'start_date': datetime.date(2015, 11, 22),
'end_date': datetime.date(2015, 12, 21),
},
{
'sign_name': 'scorpio',
'start_date': datetime.date(2015, 10, 23),
'end_date': datetime.date(2015, 11, 21),
},
{
'sign_name': 'libra',
'start_date': datetime.date(2015, 9, 23),
'end_date': datetime.date(2015, 10, 22),
},
{
'sign_name': 'virgo',
'start_date': datetime.date(2015, 8, 23),
'end_date': datetime.date(2015, 9, 22),
},
{
'sign_name': 'leo',
'start_date': datetime.date(2015, 7, 23),
'end_date': datetime.date(2015, 8, 22),
},
{
'sign_name': 'cancer',
'start_date': datetime.date(2015, 6, 21),
'end_date': datetime.date(2015, 7, 22),
},
{
'sign_name': 'gemini',
'start_date': datetime.date(2015, 5, 21),
'end_date': datetime.date(2015, 6, 20),
},
{
'sign_name': 'aries',
'start_date': datetime.date(2015, 3, 21),
'end_date': datetime.date(2015, 4, 20),
},
{
'sign_name': 'taurus',
'start_date': datetime.date(2015, 4, 21),
'end_date': datetime.date(2015, 5, 20),
},
]
target_date = datetime.date(2015, month, day)
for sign in zodiacal_signs_data:
if sign['start_date'] <= target_date <= sign['end_date']:
return sign['sign_name']
def interpret_chinese_sign(year):
chinese_zodiac_signs = [
'monkey', 'rooster', 'dog', 'pig',
'rat', 'ox', 'tiger', 'rabbit', 'dragon',
'snake', 'horse', 'sheep',
]
return chinese_zodiac_signs[year % 12]
def interpret_both_signs(day, month, year):
- return(interpret_western_sign(day, month), interpret_chinese_sign(year))
+ return interpret_western_sign(day, month), interpret_chinese_sign(year)

Наясно съм с проблема, обаче данните за датите и имената на месеците трябва да се пазят някъде при всички случаи. Разбирам, че е грозно така и мисля за по-добро решение. Ще update-на решението си, като кача решение, което не използва date.

Явор обнови решението на 07.03.2015 00:57 (преди над 9 години)

-import datetime
-
-
def interpret_western_sign(day, month):
zodiacal_signs_data = [
- {
- 'sign_name': 'aquarius',
- 'start_date': datetime.date(2015, 1, 21),
- 'end_date': datetime.date(2015, 2, 19),
- },
- {
- 'sign_name': 'pisces',
- 'start_date': datetime.date(2015, 2, 19),
- 'end_date': datetime.date(2015, 3, 20),
- },
- {
- 'sign_name': 'capricorn',
- 'start_date': datetime.date(2015, 12, 22),
- 'end_date': datetime.date(2016, 1, 20),
- },
- {
- 'sign_name': 'sagittarius',
- 'start_date': datetime.date(2015, 11, 22),
- 'end_date': datetime.date(2015, 12, 21),
- },
- {
- 'sign_name': 'scorpio',
- 'start_date': datetime.date(2015, 10, 23),
- 'end_date': datetime.date(2015, 11, 21),
- },
- {
- 'sign_name': 'libra',
- 'start_date': datetime.date(2015, 9, 23),
- 'end_date': datetime.date(2015, 10, 22),
- },
- {
- 'sign_name': 'virgo',
- 'start_date': datetime.date(2015, 8, 23),
- 'end_date': datetime.date(2015, 9, 22),
- },
- {
- 'sign_name': 'leo',
- 'start_date': datetime.date(2015, 7, 23),
- 'end_date': datetime.date(2015, 8, 22),
- },
- {
- 'sign_name': 'cancer',
- 'start_date': datetime.date(2015, 6, 21),
- 'end_date': datetime.date(2015, 7, 22),
- },
- {
- 'sign_name': 'gemini',
- 'start_date': datetime.date(2015, 5, 21),
- 'end_date': datetime.date(2015, 6, 20),
- },
- {
- 'sign_name': 'aries',
- 'start_date': datetime.date(2015, 3, 21),
- 'end_date': datetime.date(2015, 4, 20),
- },
- {
- 'sign_name': 'taurus',
- 'start_date': datetime.date(2015, 4, 21),
- 'end_date': datetime.date(2015, 5, 20),
- },
+ ('aquarius', 21, 1),
+ ('pisces', 19, 2),
+ ('aries', 21, 3),
+ ('taurus', 21, 4),
+ ('gemini', 21, 5),
+ ('cancer', 21, 6),
+ ('leo', 23, 7),
+ ('virgo', 23, 8),
+ ('libra', 23, 9),
+ ('scorpio', 23, 10),
+ ('sagittarius', 22, 11),
+ ('capricorn', 22, 12),
]
- target_date = datetime.date(2015, month, day)
+ for i in range(len(zodiacal_signs_data)):
+ month_number = i + 1
- for sign in zodiacal_signs_data:
- if sign['start_date'] <= target_date <= sign['end_date']:
- return sign['sign_name']
+ if month == month_number:
+ if day < zodiacal_signs_data[i][1]:
+ return(zodiacal_signs_data[i - 1][0])
+ else:
+ return(zodiacal_signs_data[i][0])
def interpret_chinese_sign(year):
chinese_zodiac_signs = [
'monkey', 'rooster', 'dog', 'pig',
'rat', 'ox', 'tiger', 'rabbit', 'dragon',
'snake', 'horse', 'sheep',
]
- return chinese_zodiac_signs[year % 12]
+ return chinese_zodiac_signs[year % len(chinese_zodiac_signs)]
def interpret_both_signs(day, month, year):
return interpret_western_sign(day, month), interpret_chinese_sign(year)