Решение на Астрологични забави от Иван Димитров

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

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

Резултати

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

Код

def interpret_western_sign(day, month):
all_signs = [("aries", 3, 21, 4, 20),
("taurus", 4, 21, 5, 20),
("gemini", 5, 21, 6, 20),
("cancer", 6, 21, 7, 22),
("leo", 7, 23, 8, 22),
("virgo", 8, 23, 9, 22),
("libra", 9, 23, 10, 22),
("scorpio", 10, 23, 11, 21),
("sagittarius", 11, 22, 12, 21),
("capricorn", 12, 22, 1, 20),
("aquarius", 1, 21, 2, 18),
("pisces", 2, 19, 3, 20)]
for current_sign in all_signs:
if ((month == current_sign[1] and day >= current_sign[2]) or
(month == current_sign[3] and day <= current_sign[4])):
return current_sign[0]
def interpret_chinese_sign(year):
all_signs = ["rat", "ox", "tiger", "rabbit", "dragon", "snake", "horse",
"sheep", "monkey", "rooster", "dog", "pig"]
key = (year + 20) % 12
return all_signs[key]
def interpret_both_signs(day, month, year):
return (interpret_western_sign(day, month), interpret_chinese_sign(year))

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

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

OK

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

Иван обнови решението на 10.03.2015 22:58 (преди над 9 години)

+def interpret_western_sign(day, month):
+ if day <= 0 or month <= 0 or month > 12:
+ return "Error: bad input!"
+ all_signs = [("aries", 3, 21, 4, 20),
+ ("taurus", 4, 21, 5, 20),
+ ("gemini", 5, 21, 6, 20),
+ ("cancer", 6, 21, 7, 22),
+ ("leo", 7, 23, 8, 22),
+ ("virgo", 8, 23, 9, 22),
+ ("libra", 9, 23, 10, 22),
+ ("scorpio", 10, 23, 11, 21),
+ ("sagittarius", 11, 22, 12, 21),
+ ("capricorn", 12, 22, 1, 20),
+ ("aquarius", 1, 21, 2, 18),
+ ("pisces", 2, 19, 3, 20)]
+ for current_sign in all_signs:
+ if ((month == current_sign[1] and day >= current_sign[2]) or
+ (month == current_sign[3] and day <= current_sign[4])):
+ return current_sign[0]
+
+def interpret_chinese_sign(year):
+ all_signs = {0: "rat",
+ 1: "ox",
+ 2: "tiger",
+ 3: "rabbit",
+ 4: "dragon",
+ 5: "snake",
+ 6: "horse",
+ 7: "sheep",
+ 8: "monkey",
+ 9: "rooster",
+ 10: "dog",
+ 11: "pig"}
+ key = (year + 20) % 12
+ return all_signs.get(key)
+
+def interpret_both_signs(day, month, year):
+ return (interpret_western_sign(day, month), interpret_chinese_sign(year))

Премахни тази проверка:

    if day <= 0 or month <= 0 or month > 12:
        return "Error: bad input!"

Изрично сме ви казали да очаквате само коректно подадени данни :)

Също така, помисли коя колекция е по-подходяща от речник в interpret_chinese_sign :)

Иван обнови решението на 11.03.2015 00:51 (преди над 9 години)

def interpret_western_sign(day, month):
- if day <= 0 or month <= 0 or month > 12:
- return "Error: bad input!"
all_signs = [("aries", 3, 21, 4, 20),
("taurus", 4, 21, 5, 20),
("gemini", 5, 21, 6, 20),
("cancer", 6, 21, 7, 22),
("leo", 7, 23, 8, 22),
("virgo", 8, 23, 9, 22),
("libra", 9, 23, 10, 22),
("scorpio", 10, 23, 11, 21),
("sagittarius", 11, 22, 12, 21),
("capricorn", 12, 22, 1, 20),
("aquarius", 1, 21, 2, 18),
("pisces", 2, 19, 3, 20)]
for current_sign in all_signs:
if ((month == current_sign[1] and day >= current_sign[2]) or
(month == current_sign[3] and day <= current_sign[4])):
return current_sign[0]
def interpret_chinese_sign(year):
- all_signs = {0: "rat",
- 1: "ox",
- 2: "tiger",
- 3: "rabbit",
- 4: "dragon",
- 5: "snake",
- 6: "horse",
- 7: "sheep",
- 8: "monkey",
- 9: "rooster",
- 10: "dog",
- 11: "pig"}
+ all_signs = ["rat", "ox", "tiger", "rabbit", "dragon", "snake", "horse",
+ "sheep", "monkey", "rooster", "dog", "pig"]
key = (year + 20) % 12
- return all_signs.get(key)
+ return all_signs[key]
def interpret_both_signs(day, month, year):
return (interpret_western_sign(day, month), interpret_chinese_sign(year))