Решение на Астрологични забави от Николай Велков

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

Към профила на Николай Велков

Резултати

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

Код

def calculate_sign(day, fir_sign, critic_day, sec_sign):
if day < critic_day:
return fir_sign
else:
return sec_sign
def interpret_western_sign(day, month):
signs = (
"capricorn", "aquarius", "pisces", "aries",
"taurus", "gemini", "cancer", "leo", "virgo",
"libra", "scorpio", "sagittarius"
)
sign_info = [
(signs[0], 21, signs[1]),
(signs[1], 19, signs[2]),
(signs[2], 21, signs[3]),
(signs[3], 21, signs[4]),
(signs[4], 21, signs[5]),
(signs[5], 21, signs[6]),
(signs[6], 23, signs[7]),
(signs[7], 23, signs[8]),
(signs[8], 23, signs[9]),
(signs[9], 23, signs[10]),
(signs[10], 22, signs[11]),
(signs[11], 22, signs[0])
]
month_tup = sign_info[month-1]
return calculate_sign(day, month_tup[0], month_tup[1], month_tup[2])
def interpret_chinese_sign(year):
index = abs((year - 1900) % 12)
sign_dict = [
"rat", "ox", "tiger", "rabbit",
"dragon", "snake", "horse", "sheep",
"monkey", "rooster", "dog", "pig"
]
return sign_dict[index]
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 коментар)

Николай обнови решението на 09.03.2015 17:31 (преди над 9 години)

+def calc_sign(day, fir_sign, critic_day, sec_sign):
+ if day < critic_day:
+ return fir_sign
+ else:
+ return sec_sign
+
+
+def interpret_western_sign(day, month):
+ signs = (
+ "capricorn", "aquarius", "pisces", "aries",
+ "taurus", "gemini", "cancer", "leo", "virgo",
+ "libra", "scorpio", "sagittarius"
+ )
+ sign_dict = {
+ 1: (signs[0], 21, signs[1]),
+ 2: (signs[1], 19, signs[2]),
+ 3: (signs[2], 21, signs[3]),
+ 4: (signs[3], 21, signs[4]),
+ 5: (signs[4], 21, signs[5]),
+ 6: (signs[5], 21, signs[6]),
+ 7: (signs[6], 23, signs[7]),
+ 8: (signs[7], 23, signs[8]),
+ 9: (signs[8], 23, signs[9]),
+ 10: (signs[9], 23, signs[10]),
+ 11: (signs[10], 22, signs[11]),
+ 12: (signs[11], 22, signs[0])
+ }
+
+ month_tup = sign_dict[month]
+
+ return calc_sign(day, month_tup[0], month_tup[1], month_tup[2])
+
+
+def interpret_chinese_sign(year):
+ index = abs((year - 1900) % 12)
+
+ sign_dict = {
+ 0: "rat", 1: "ox", 2: "tiger", 3: "rabbit",
+ 4: "dragon", 5: "snake", 6: "horse", 7: "sheep",
+ 8: "monkey", 9: "rooster", 10: "dog", 11: "pig"
+ }
+
+ return sign_dict[index]
+
+
+def interpret_both_signs(day, month, year):
+ tup = (interpret_western_sign(day, month), interpret_chinese_sign(year))
+
+ return tup
  • по-добрo име на функция и аргументи би било: def calculate_sign(day, first_sign, critic_day, second_sign): PEP 20 -- The Zen of Python
  • Няма нужда да присвояваш на tup и след това да връщаш tup в interpret_both_signs. Просто return ...
  • речник с ключове последователни числа, не ти ли напомня на един друг вид наредена структура от данни?

Николай обнови решението на 10.03.2015 19:19 (преди над 9 години)

-def calc_sign(day, fir_sign, critic_day, sec_sign):
+def calculate_sign(day, fir_sign, critic_day, sec_sign):
if day < critic_day:
return fir_sign
else:
return sec_sign
def interpret_western_sign(day, month):
signs = (
"capricorn", "aquarius", "pisces", "aries",
"taurus", "gemini", "cancer", "leo", "virgo",
"libra", "scorpio", "sagittarius"
)
- sign_dict = {
- 1: (signs[0], 21, signs[1]),
- 2: (signs[1], 19, signs[2]),
- 3: (signs[2], 21, signs[3]),
- 4: (signs[3], 21, signs[4]),
- 5: (signs[4], 21, signs[5]),
- 6: (signs[5], 21, signs[6]),
- 7: (signs[6], 23, signs[7]),
- 8: (signs[7], 23, signs[8]),
- 9: (signs[8], 23, signs[9]),
- 10: (signs[9], 23, signs[10]),
- 11: (signs[10], 22, signs[11]),
- 12: (signs[11], 22, signs[0])
- }
+ sign_info = [
+ (signs[0], 21, signs[1]),
+ (signs[1], 19, signs[2]),
+ (signs[2], 21, signs[3]),
+ (signs[3], 21, signs[4]),
+ (signs[4], 21, signs[5]),
+ (signs[5], 21, signs[6]),
+ (signs[6], 23, signs[7]),
+ (signs[7], 23, signs[8]),
+ (signs[8], 23, signs[9]),
+ (signs[9], 23, signs[10]),
+ (signs[10], 22, signs[11]),
+ (signs[11], 22, signs[0])
+ ]
- month_tup = sign_dict[month]
+ month_tup = sign_info[month-1]
- return calc_sign(day, month_tup[0], month_tup[1], month_tup[2])
+ return calculate_sign(day, month_tup[0], month_tup[1], month_tup[2])
def interpret_chinese_sign(year):
index = abs((year - 1900) % 12)
- sign_dict = {
- 0: "rat", 1: "ox", 2: "tiger", 3: "rabbit",
- 4: "dragon", 5: "snake", 6: "horse", 7: "sheep",
- 8: "monkey", 9: "rooster", 10: "dog", 11: "pig"
- }
+ sign_dict = [
+ "rat", "ox", "tiger", "rabbit",
+ "dragon", "snake", "horse", "sheep",
+ "monkey", "rooster", "dog", "pig"
+ ]
return sign_dict[index]
def interpret_both_signs(day, month, year):
- tup = (interpret_western_sign(day, month), interpret_chinese_sign(year))
-
+ return (interpret_western_sign(day, month), interpret_chinese_sign(year))
- return tup