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

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

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

Резултати

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

Код

"""
Determines the zodiac sign by day and month
through array search
"""
ZODIAC_SIGNS = [(119, 'Козирог'), (218, 'Водолей'), (320, 'Риби'),
(420, 'Овен'), (520, 'Телец'), (620, 'Близнаци'),
(721, 'Рак'), (822, 'Лъв'), (922, 'Дева'), (1022, 'Везни'),
(1121, 'Скорпион'), (1221, 'Стрелец'), (1231, 'Козирог')]
def what_is_my_sign(day, month):
date_number = month*100 + day
for current_sign in ZODIAC_SIGNS:
if date_number <= current_sign[0]:
return current_sign[1]

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

...............
----------------------------------------------------------------------
Ran 15 tests in 0.001s

OK

История (7 версии и 6 коментара)

Марио обнови решението на 28.02.2013 22:15 (преди около 11 години)

+"""
+Dtermines the zodiac sign by day and month
+through associative array search
+"""
+
+zodiac_signs = [(119, 'Козирог'), (218, 'Водолей'), (320, 'Риби'), (420, 'Овен'), (520, 'Телец'),
+ (620, 'Близнаци'), (721, 'Рак'), (822, 'Лъв'), (922, 'Дева'), (1022, 'Везни'),
+ (1121, 'Скорпион'), (1221, 'Стрелец'), (1231, 'Козирог')]
+
+def what_is_my_sign(day, month):
+ if(month<1 or month>12) or (day<1 or day>31):
+ print ("You sure you/'re from this planet?")
+ else:
+ date_number = month*100+day
+ for curent_sign in zodiac_signs:
+ if date_number <= curent_sign[0]:
+ return curent_sign[1]
+
+assert what_is_my_sign(5, 8) == 'Лъв'
+assert what_is_my_sign(29, 1) == 'Водолей'
+assert what_is_my_sign(30, 6) == 'Рак'
+assert what_is_my_sign(31, 5) == 'Близнаци'
+assert what_is_my_sign(2, 2) == 'Водолей'
+assert what_is_my_sign(month=5, day=8) == 'Телец'
+assert what_is_my_sign(9, month=1) == 'Козирог'

Марио обнови решението на 28.02.2013 22:18 (преди около 11 години)

"""
Dtermines the zodiac sign by day and month
through associative array search
"""
zodiac_signs = [(119, 'Козирог'), (218, 'Водолей'), (320, 'Риби'), (420, 'Овен'), (520, 'Телец'),
(620, 'Близнаци'), (721, 'Рак'), (822, 'Лъв'), (922, 'Дева'), (1022, 'Везни'),
(1121, 'Скорпион'), (1221, 'Стрелец'), (1231, 'Козирог')]
def what_is_my_sign(day, month):
if(month<1 or month>12) or (day<1 or day>31):
print ("You sure you/'re from this planet?")
else:
date_number = month*100+day
- for curent_sign in zodiac_signs:
- if date_number <= curent_sign[0]:
- return curent_sign[1]
+ for current_sign in zodiac_signs:
+ if date_number <= current_sign[0]:
+ return current_sign[1]
assert what_is_my_sign(5, 8) == 'Лъв'
assert what_is_my_sign(29, 1) == 'Водолей'
assert what_is_my_sign(30, 6) == 'Рак'
assert what_is_my_sign(31, 5) == 'Близнаци'
assert what_is_my_sign(2, 2) == 'Водолей'
assert what_is_my_sign(month=5, day=8) == 'Телец'
assert what_is_my_sign(9, month=1) == 'Козирог'

Марио обнови решението на 28.02.2013 22:26 (преди около 11 години)

"""
Dtermines the zodiac sign by day and month
through associative array search
"""
zodiac_signs = [(119, 'Козирог'), (218, 'Водолей'), (320, 'Риби'), (420, 'Овен'), (520, 'Телец'),
(620, 'Близнаци'), (721, 'Рак'), (822, 'Лъв'), (922, 'Дева'), (1022, 'Везни'),
(1121, 'Скорпион'), (1221, 'Стрелец'), (1231, 'Козирог')]
def what_is_my_sign(day, month):
- if(month<1 or month>12) or (day<1 or day>31):
- print ("You sure you/'re from this planet?")
+ date_number = month*100+day
- else:
+ for curent_sign in zodiac_signs:
- date_number = month*100+day
+ if date_number <= curent_sign[0]:
- for current_sign in zodiac_signs:
+ return curent_sign[1]
- if date_number <= current_sign[0]:
- return current_sign[1]
-
-assert what_is_my_sign(5, 8) == 'Лъв'
-assert what_is_my_sign(29, 1) == 'Водолей'
-assert what_is_my_sign(30, 6) == 'Рак'
-assert what_is_my_sign(31, 5) == 'Близнаци'
-assert what_is_my_sign(2, 2) == 'Водолей'
-assert what_is_my_sign(month=5, day=8) == 'Телец'
-assert what_is_my_sign(9, month=1) == 'Козирог'

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

"""
Dtermines the zodiac sign by day and month
through associative array search
"""
zodiac_signs = [(119, 'Козирог'), (218, 'Водолей'), (320, 'Риби'), (420, 'Овен'), (520, 'Телец'),
(620, 'Близнаци'), (721, 'Рак'), (822, 'Лъв'), (922, 'Дева'), (1022, 'Везни'),
(1121, 'Скорпион'), (1221, 'Стрелец'), (1231, 'Козирог')]
def what_is_my_sign(day, month):
date_number = month*100+day
- for curent_sign in zodiac_signs:
- if date_number <= curent_sign[0]:
+ for current_sign in zodiac_signs:
- return curent_sign[1]
+ if date_number <= current_sign[0]:
+ return current_sign[1]

Марио обнови решението на 28.02.2013 22:41 (преди около 11 години)

"""
-Dtermines the zodiac sign by day and month
-through associative array search
+Determines the zodiac sign by day and month
+through array search
"""
zodiac_signs = [(119, 'Козирог'), (218, 'Водолей'), (320, 'Риби'), (420, 'Овен'), (520, 'Телец'),
(620, 'Близнаци'), (721, 'Рак'), (822, 'Лъв'), (922, 'Дева'), (1022, 'Везни'),
(1121, 'Скорпион'), (1221, 'Стрелец'), (1231, 'Козирог')]
def what_is_my_sign(day, month):
- date_number = month*100+day
+ date_number = month*100 + day
for current_sign in zodiac_signs:
if date_number <= current_sign[0]:
return current_sign[1]

CapWords ON, Tab сетнат на 4 интервала. А защо за всичко се използва този underscoring, надали е readability, camelCase добре се справя, а за красота сякаш повече предпочитам унгарската :smile:. Може ли да ползваме assert за debug тестване?

Марио обнови решението на 01.03.2013 18:27 (преди около 11 години)

"""
Determines the zodiac sign by day and month
through array search
"""
-zodiac_signs = [(119, 'Козирог'), (218, 'Водолей'), (320, 'Риби'), (420, 'Овен'), (520, 'Телец'),
- (620, 'Близнаци'), (721, 'Рак'), (822, 'Лъв'), (922, 'Дева'), (1022, 'Везни'),
- (1121, 'Скорпион'), (1221, 'Стрелец'), (1231, 'Козирог')]
+ZODIAC_SIGNS = [(119, 'Козирог'), (218, 'Водолей'), (320, 'Риби'), (420, 'Овен'), (520, 'Телец'),
+ (620, 'Близнаци'), (721, 'Рак'), (822, 'Лъв'), (922, 'Дева'), (1022, 'Везни'),
+ (1121, 'Скорпион'), (1221, 'Стрелец'), (1231, 'Козирог')]
def what_is_my_sign(day, month):
- date_number = month*100 + day
- for current_sign in zodiac_signs:
+ date_number = month*100 + day
- if date_number <= current_sign[0]:
+ for current_sign in ZODIAC_SIGNS:
- return current_sign[1]
+ if date_number <= current_sign[0]:
+ return current_sign[1]

Корените на Python са в C света. А там долни черти не се пестят.

Би могъл да използваш assert, за да валидираш, че кода ти няма да се изпочупи, ако се извика с невалидни данни. За тестване е по-добре да използваш unit test-ове. Ще ги предадем достатъчно скоро. Ако си нетърпелив - разгледай примерния тест във форума/github.

Относно унгарската - аз лично не съм виждал някой да я ползва извън Microsoft. За нея виждам употреба единствено в статично типизиран език, за който не съществува IDE. А такива не са останали твърде много. [Любезно ще пропусна това, че използва "красота" и "унгарска нотация" в едно изречение.]

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

"""
-Determines the zodiac sign by day and month
+Determines the zodiac sign by day and month
through array search
"""
-ZODIAC_SIGNS = [(119, 'Козирог'), (218, 'Водолей'), (320, 'Риби'), (420, 'Овен'), (520, 'Телец'),
- (620, 'Близнаци'), (721, 'Рак'), (822, 'Лъв'), (922, 'Дева'), (1022, 'Везни'),
+ZODIAC_SIGNS = [(119, 'Козирог'), (218, 'Водолей'), (320, 'Риби'),
+ (420, 'Овен'), (520, 'Телец'), (620, 'Близнаци'),
+ (721, 'Рак'), (822, 'Лъв'), (922, 'Дева'), (1022, 'Везни'),
(1121, 'Скорпион'), (1221, 'Стрелец'), (1231, 'Козирог')]
-
+
+
def what_is_my_sign(day, month):
date_number = month*100 + day
for current_sign in ZODIAC_SIGNS:
if date_number <= current_sign[0]:
- return current_sign[1]
+ return current_sign[1]