Created
June 7, 2016 12:17
-
-
Save rmgimenez/f1d3abe9c65ed98c2f593c6c734e2e25 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from time import time | |
start = time() | |
''' | |
Retorna true se o ano é bissexto e false se não for | |
''' | |
def is_leap_year(ano): | |
if (ano % 2 != 0): | |
return False | |
else: | |
if (ano % 100 == 0): | |
if (ano % 400 == 0): | |
return True | |
else: | |
return False | |
else: | |
if (ano % 4 == 0): | |
return True | |
else: | |
return False | |
dia_semana = 1 # 1 = segunda-feira / 7 = domingo | |
total_domingo_inico_mes = 0 | |
def proximo_dia_semana(dia_atual): | |
if dia_atual == 7: | |
return 1 | |
else: | |
return dia_atual + 1 | |
for y in range(1900, 2001): | |
for m in range(1,13): | |
if m in (4,6,9,11): # meses com 30 dias | |
for d in range(1,31): | |
if (d == 1 and y != 1900 and dia_semana == 7): | |
total_domingo_inico_mes += 1 | |
dia_semana = proximo_dia_semana(dia_semana) | |
elif m in (1,3,5,7,8,10,12): # meses com 31 dias | |
for d in range(1,32): | |
if (d == 1 and y != 1900 and dia_semana == 7): | |
total_domingo_inico_mes += 1 | |
dia_semana = proximo_dia_semana(dia_semana) | |
else: # fevereiro | |
if is_leap_year(y): | |
for d in range(1,30): # se o ano for bissexto fevereiro terá 29 dias | |
if (d == 1 and y != 1900 and dia_semana == 7): | |
total_domingo_inico_mes += 1 | |
dia_semana = proximo_dia_semana(dia_semana) | |
else: | |
for d in range(1,29): # se o ano não for bissexto fevereiro terá 28 dias | |
if (d == 1 and y != 1900 and dia_semana == 7): | |
total_domingo_inico_mes += 1 | |
dia_semana = proximo_dia_semana(dia_semana) | |
print(total_domingo_inico_mes) | |
print("Time: {0} secs".format(time()-start)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment