Решение на Хороскоп от Моника Ефтимова

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

Към профила на Моника Ефтимова

Резултати

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

Код

Signs = [
("Овен", (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))]
Month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def starts(start):
days_start = 0
for i in range(start[1]-1):
days_start += Month_days[i]
days_start += start[0]
return days_start
def ends(start, end):
days_start = starts(start)
return days_start + Month_days[start[1]-1] - start[0] + end[0]
def what_is_my_sign(day, month):
days_from_beginning = starts([day, month])
sign = None
if((day >= 22 and month == 12) or (day <= 19 and month == 1)):
sign = Signs[9][0]
else:
for i in range(len(Signs)):
if(days_from_beginning >= starts(Signs[i][1]) and
days_from_beginning <= ends(Signs[i][1], Signs[i][2])):
sign = Signs[i][0]
break
return sign

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

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

OK

История (8 версии и 3 коментара)

Моника обнови решението на 02.03.2013 07:39 (преди над 11 години)

+def what_is_my_sign(month, day):
+ if (month >= 3 and day >= 21 or month <= 4 and day <= 20):
+ print("Овен")
+ elif (month >= 4 and day >= 21 or month <= 5 and day <= 20):
+ print("Телец")
+ elif (month >= 5 and day >= 21 or month <= 6 and day <= 20):
+ print("Близнаци")
+ elif (month >= 6 and day >= 21 or month <= 7 and day <= 21):
+ print("Рак")
+ elif (month >= 7 and day >= 22 or month <= 8 and day <= 22):
+ print("Лъв")
+ elif (month >= 8 and day >= 23 or month <= 9 and day <= 22):
+ print("Дева")
+ elif (month >= 9 and day >= 23 or month <= 10 and day <= 22):
+ print("Везни")
+ elif (month >= 10 and day >= 23 or month <= 11 and day <= 21):
+ print("Скорпион")
+ elif (month >= 11 and day >= 22 or month <= 12 and day <= 21):
+ print("Стрелец")
+ elif (month >= 22 and day >= 12 or month <= 1 and day <= 19):
+ print("Козирог")
+ elif (month >= 1 and day >= 20 or month <= 2 and day <= 18):
+ print("Водолей")
+ elif (month >= 2 and day >= 19 or month <= 3 and day <= 20):
+ print("Риби")

Помисли как да го направиш без толкова много if-ове. Така определено не е елегантно.

Освен това си пусни примерния тест -- ще видиш че така не минава защото е нужно функцията да връща (return) стойност, а не да я принтира.

Моника обнови решението на 03.03.2013 11:16 (преди над 11 години)

-def what_is_my_sign(month, day):
- if (month >= 3 and day >= 21 or month <= 4 and day <= 20):
+def what_is_my_sign(day, month):
- print("Овен")
+ date_ranges = [
- elif (month >= 4 and day >= 21 or month <= 5 and day <= 20):
+ {"start_date": [21, 3], "end_date": [20, 4]},
- print("Телец")
+ {"start_date": [21, 4], "end_date": [20, 5]},
- elif (month >= 5 and day >= 21 or month <= 6 and day <= 20):
+ {"start_date": [21, 5], "end_date": [20, 6]},
- print("Близнаци")
+ {"start_date": [21, 6], "end_date": [21, 7]},
- elif (month >= 6 and day >= 21 or month <= 7 and day <= 21):
+ {"start_date": [22, 7], "end_date": [22, 8]},
- print("Рак")
+ {"start_date": [23, 8], "end_date": [22, 9]},
- elif (month >= 7 and day >= 22 or month <= 8 and day <= 22):
+ {"start_date": [23, 9], "end_date": [22, 10]},
- print("Лъв")
+ {"start_date": [23, 10], "end_date": [21, 11]},
- elif (month >= 8 and day >= 23 or month <= 9 and day <= 22):
+ {"start_date": [22, 11], "end_date": [21, 12]},
- print("Дева")
+ {"start_date": [22, 12], "end_date": [19, 1]},
- elif (month >= 9 and day >= 23 or month <= 10 and day <= 22):
+ {"start_date": [20, 1], "end_date": [18, 2]},
- print("Везни")
+ {"start_date": [19, 2], "end_date": [20, 3]}]
- elif (month >= 10 and day >= 23 or month <= 11 and day <= 21):
+ signs = [
- print("Скорпион")
+ "Овен",
- elif (month >= 11 and day >= 22 or month <= 12 and day <= 21):
+ "Телец",
- print("Стрелец")
+ "Близнаци",
- elif (month >= 22 and day >= 12 or month <= 1 and day <= 19):
+ "Рак",
- print("Козирог")
+ "Лъв",
- elif (month >= 1 and day >= 20 or month <= 2 and day <= 18):
+ "Дева",
- print("Водолей")
+ "Везни",
- elif (month >= 2 and day >= 19 or month <= 3 and day <= 20):
+ "Скорпион",
- print("Риби")
+ "Стрелец",
+ "Козирог",
+ "Водолей",
+ "Риби"]
+ sign = None
+ for i in range(len(date_ranges)):
+ day_begin = date_ranges[i]["start_date"][0]
+ month_begin = date_ranges[i]["start_date"][1]
+ day_end = date_ranges[i]["end_date"][0]
+ month_end = date_ranges[i]["end_date"][1]
+ if((month >= month_begin and month <= month_end) and
+ ((month == month_begin and day >= day_begin) or
+ (month == month_end and day <= day_end))):
+ sign = signs[i]
+ break
+ return sign

Моника обнови решението на 03.03.2013 11:32 (преди над 11 години)

def what_is_my_sign(day, month):
date_ranges = [
{"start_date": [21, 3], "end_date": [20, 4]},
{"start_date": [21, 4], "end_date": [20, 5]},
{"start_date": [21, 5], "end_date": [20, 6]},
{"start_date": [21, 6], "end_date": [21, 7]},
{"start_date": [22, 7], "end_date": [22, 8]},
{"start_date": [23, 8], "end_date": [22, 9]},
{"start_date": [23, 9], "end_date": [22, 10]},
{"start_date": [23, 10], "end_date": [21, 11]},
{"start_date": [22, 11], "end_date": [21, 12]},
{"start_date": [22, 12], "end_date": [19, 1]},
{"start_date": [20, 1], "end_date": [18, 2]},
{"start_date": [19, 2], "end_date": [20, 3]}]
signs = [
"Овен",
"Телец",
"Близнаци",
"Рак",
"Лъв",
"Дева",
"Везни",
"Скорпион",
"Стрелец",
"Козирог",
"Водолей",
"Риби"]
sign = None
for i in range(len(date_ranges)):
day_begin = date_ranges[i]["start_date"][0]
month_begin = date_ranges[i]["start_date"][1]
day_end = date_ranges[i]["end_date"][0]
month_end = date_ranges[i]["end_date"][1]
- if((month >= month_begin and month <= month_end) and
+ if(((month >= month_begin and month <= month_end) or
+ (month >= month_end and month <= month_begin)) and
((month == month_begin and day >= day_begin) or
(month == month_end and day <= day_end))):
sign = signs[i]
break
return sign

Така е доста по-добре. Един начин по който можеш да опростиш още функцията е да забравиш за съществуването на месеци и да мислиш само за дни от началото на годината. Така няма да ти трябва четириредов if например.

Също можеш да изнесеш константите (signs, date_ranges) извън функцията защото не е нужно те да се декларират наново при всяко извикване.

PS: Нещо което не сме ви казвали е че за константи практиката е имената на променливите да са с главни букви.

Моника обнови решението на 04.03.2013 12:45 (преди над 11 години)

+Month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
+
+
+def starts(start):
+ days_start = 0
+ for i in range(start[1]-1):
+ days_start += Month_days[i]
+ days_start += start[0]
+ return days_start
+
+
+def ends(start, end):
+ days_start = starts(start)
+ return days_start + Month_days[start[1]-1] - start[0] + end[0]
+Signs = [
+ "Овен",
+ "Телец",
+ "Близнаци",
+ "Рак",
+ "Лъв",
+ "Дева",
+ "Везни",
+ "Скорпион",
+ "Стрелец",
+ "Козирог",
+ "Водолей",
+ "Риби"]
+date_ranges = [
+ {"start_days": [21, 3], "end_days": [20, 4]},
+ {"start_days": [21, 4], "end_days": [20, 5]},
+ {"start_days": [21, 5], "end_days": [20, 6]},
+ {"start_days": [21, 6], "end_days": [21, 7]},
+ {"start_days": [22, 7], "end_days": [22, 8]},
+ {"start_days": [23, 8], "end_days": [22, 9]},
+ {"start_days": [23, 9], "end_days": [22, 10]},
+ {"start_days": [23, 10], "end_days": [21, 11]},
+ {"start_days": [22, 11], "end_days": [21, 12]},
+ {"start_days": [22, 12], "end_days": [19, 1]},
+ {"start_days": [20, 1], "end_days": [18, 2]},
+ {"start_days": [19, 2], "end_days": [20, 3]}]
+for i in range(len(date_ranges)):
+ date_ranges[i]["end_date"] = ends(date_ranges[i]["start_date"],
+ date_ranges[i]["end_date"])
+ date_ranges[i]["start_date"] = starts(date_ranges[i]["start_date"])
+
+
def what_is_my_sign(day, month):
- date_ranges = [
- {"start_date": [21, 3], "end_date": [20, 4]},
- {"start_date": [21, 4], "end_date": [20, 5]},
- {"start_date": [21, 5], "end_date": [20, 6]},
- {"start_date": [21, 6], "end_date": [21, 7]},
- {"start_date": [22, 7], "end_date": [22, 8]},
- {"start_date": [23, 8], "end_date": [22, 9]},
- {"start_date": [23, 9], "end_date": [22, 10]},
- {"start_date": [23, 10], "end_date": [21, 11]},
- {"start_date": [22, 11], "end_date": [21, 12]},
- {"start_date": [22, 12], "end_date": [19, 1]},
- {"start_date": [20, 1], "end_date": [18, 2]},
- {"start_date": [19, 2], "end_date": [20, 3]}]
- signs = [
- "Овен",
- "Телец",
- "Близнаци",
- "Рак",
- "Лъв",
- "Дева",
- "Везни",
- "Скорпион",
- "Стрелец",
- "Козирог",
- "Водолей",
- "Риби"]
+ days_from_beginning = starts([day, month])
sign = None
for i in range(len(date_ranges)):
- day_begin = date_ranges[i]["start_date"][0]
- month_begin = date_ranges[i]["start_date"][1]
- day_end = date_ranges[i]["end_date"][0]
- month_end = date_ranges[i]["end_date"][1]
- if(((month >= month_begin and month <= month_end) or
- (month >= month_end and month <= month_begin)) and
- ((month == month_begin and day >= day_begin) or
- (month == month_end and day <= day_end))):
- sign = signs[i]
+ if((day >= 22 and month == 12) or (day <= 19 and month == 1)):
+ sign = Signs[9]
+ break
+ elif(days_from_beginning >= date_ranges[i]["start_days"] and
+ days_from_beginning <= date_ranges[i]["end_days"]):
+ sign = Signs[i]
break
return sign

Моника обнови решението на 04.03.2013 12:46 (преди над 11 години)

Month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def starts(start):
days_start = 0
for i in range(start[1]-1):
days_start += Month_days[i]
days_start += start[0]
return days_start
def ends(start, end):
days_start = starts(start)
return days_start + Month_days[start[1]-1] - start[0] + end[0]
Signs = [
"Овен",
"Телец",
"Близнаци",
"Рак",
"Лъв",
"Дева",
"Везни",
"Скорпион",
"Стрелец",
"Козирог",
"Водолей",
"Риби"]
date_ranges = [
{"start_days": [21, 3], "end_days": [20, 4]},
{"start_days": [21, 4], "end_days": [20, 5]},
{"start_days": [21, 5], "end_days": [20, 6]},
{"start_days": [21, 6], "end_days": [21, 7]},
{"start_days": [22, 7], "end_days": [22, 8]},
{"start_days": [23, 8], "end_days": [22, 9]},
{"start_days": [23, 9], "end_days": [22, 10]},
{"start_days": [23, 10], "end_days": [21, 11]},
{"start_days": [22, 11], "end_days": [21, 12]},
{"start_days": [22, 12], "end_days": [19, 1]},
{"start_days": [20, 1], "end_days": [18, 2]},
{"start_days": [19, 2], "end_days": [20, 3]}]
for i in range(len(date_ranges)):
- date_ranges[i]["end_date"] = ends(date_ranges[i]["start_date"],
- date_ranges[i]["end_date"])
- date_ranges[i]["start_date"] = starts(date_ranges[i]["start_date"])
+ date_ranges[i]["end_days"] = ends(date_ranges[i]["start_days"],
+ date_ranges[i]["end_days"])
+ date_ranges[i]["start_days"] = starts(date_ranges[i]["start_date"])
def what_is_my_sign(day, month):
days_from_beginning = starts([day, month])
sign = None
for i in range(len(date_ranges)):
if((day >= 22 and month == 12) or (day <= 19 and month == 1)):
sign = Signs[9]
break
elif(days_from_beginning >= date_ranges[i]["start_days"] and
days_from_beginning <= date_ranges[i]["end_days"]):
sign = Signs[i]
break
return sign

Моника обнови решението на 04.03.2013 12:47 (преди над 11 години)

Month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def starts(start):
days_start = 0
for i in range(start[1]-1):
days_start += Month_days[i]
days_start += start[0]
return days_start
def ends(start, end):
days_start = starts(start)
return days_start + Month_days[start[1]-1] - start[0] + end[0]
Signs = [
"Овен",
"Телец",
"Близнаци",
"Рак",
"Лъв",
"Дева",
"Везни",
"Скорпион",
"Стрелец",
"Козирог",
"Водолей",
"Риби"]
date_ranges = [
{"start_days": [21, 3], "end_days": [20, 4]},
{"start_days": [21, 4], "end_days": [20, 5]},
{"start_days": [21, 5], "end_days": [20, 6]},
{"start_days": [21, 6], "end_days": [21, 7]},
{"start_days": [22, 7], "end_days": [22, 8]},
{"start_days": [23, 8], "end_days": [22, 9]},
{"start_days": [23, 9], "end_days": [22, 10]},
{"start_days": [23, 10], "end_days": [21, 11]},
{"start_days": [22, 11], "end_days": [21, 12]},
{"start_days": [22, 12], "end_days": [19, 1]},
{"start_days": [20, 1], "end_days": [18, 2]},
{"start_days": [19, 2], "end_days": [20, 3]}]
for i in range(len(date_ranges)):
date_ranges[i]["end_days"] = ends(date_ranges[i]["start_days"],
date_ranges[i]["end_days"])
- date_ranges[i]["start_days"] = starts(date_ranges[i]["start_date"])
+ date_ranges[i]["start_days"] = starts(date_ranges[i]["start_days"])
def what_is_my_sign(day, month):
days_from_beginning = starts([day, month])
sign = None
for i in range(len(date_ranges)):
if((day >= 22 and month == 12) or (day <= 19 and month == 1)):
sign = Signs[9]
break
elif(days_from_beginning >= date_ranges[i]["start_days"] and
days_from_beginning <= date_ranges[i]["end_days"]):
sign = Signs[i]
break
return sign

Моника обнови решението на 04.03.2013 14:30 (преди над 11 години)

+Signs = [
+ ("Овен", (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))]
Month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def starts(start):
days_start = 0
for i in range(start[1]-1):
days_start += Month_days[i]
days_start += start[0]
return days_start
def ends(start, end):
days_start = starts(start)
return days_start + Month_days[start[1]-1] - start[0] + end[0]
-Signs = [
- "Овен",
- "Телец",
- "Близнаци",
- "Рак",
- "Лъв",
- "Дева",
- "Везни",
- "Скорпион",
- "Стрелец",
- "Козирог",
- "Водолей",
- "Риби"]
-date_ranges = [
- {"start_days": [21, 3], "end_days": [20, 4]},
- {"start_days": [21, 4], "end_days": [20, 5]},
- {"start_days": [21, 5], "end_days": [20, 6]},
- {"start_days": [21, 6], "end_days": [21, 7]},
- {"start_days": [22, 7], "end_days": [22, 8]},
- {"start_days": [23, 8], "end_days": [22, 9]},
- {"start_days": [23, 9], "end_days": [22, 10]},
- {"start_days": [23, 10], "end_days": [21, 11]},
- {"start_days": [22, 11], "end_days": [21, 12]},
- {"start_days": [22, 12], "end_days": [19, 1]},
- {"start_days": [20, 1], "end_days": [18, 2]},
- {"start_days": [19, 2], "end_days": [20, 3]}]
-for i in range(len(date_ranges)):
- date_ranges[i]["end_days"] = ends(date_ranges[i]["start_days"],
- date_ranges[i]["end_days"])
- date_ranges[i]["start_days"] = starts(date_ranges[i]["start_days"])
def what_is_my_sign(day, month):
days_from_beginning = starts([day, month])
sign = None
- for i in range(len(date_ranges)):
+ for i in range(len(Signs)):
if((day >= 22 and month == 12) or (day <= 19 and month == 1)):
- sign = Signs[9]
+ sign = Signs[9][0]
break
- elif(days_from_beginning >= date_ranges[i]["start_days"] and
- days_from_beginning <= date_ranges[i]["end_days"]):
- sign = Signs[i]
+ elif(days_from_beginning >= starts(Signs[i][1]) and
+ days_from_beginning <= ends(Signs[i][1], Signs[i][2])):
+ sign = Signs[i][0]
break
return sign

Моника обнови решението на 04.03.2013 14:45 (преди над 11 години)

Signs = [
("Овен", (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))]
Month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def starts(start):
days_start = 0
for i in range(start[1]-1):
days_start += Month_days[i]
days_start += start[0]
return days_start
def ends(start, end):
days_start = starts(start)
return days_start + Month_days[start[1]-1] - start[0] + end[0]
def what_is_my_sign(day, month):
days_from_beginning = starts([day, month])
sign = None
- for i in range(len(Signs)):
- if((day >= 22 and month == 12) or (day <= 19 and month == 1)):
- sign = Signs[9][0]
- break
- elif(days_from_beginning >= starts(Signs[i][1]) and
- days_from_beginning <= ends(Signs[i][1], Signs[i][2])):
- sign = Signs[i][0]
- break
+ if((day >= 22 and month == 12) or (day <= 19 and month == 1)):
+ sign = Signs[9][0]
+ else:
+ for i in range(len(Signs)):
+ if(days_from_beginning >= starts(Signs[i][1]) and
+ days_from_beginning <= ends(Signs[i][1], Signs[i][2])):
+ sign = Signs[i][0]
+ break
return sign