-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathano_bissexto.py
executable file
·65 lines (58 loc) · 2.14 KB
/
ano_bissexto.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def isLeapYear(year):
if (year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0):
return True
else:
return False
def daysInMonth(year,month):
if month in (1,3,5,7,8,10,12):
return 31
if month == 2:
if isLeapYear(year):
return 29
else:
return 28
return 30
def nextDay(year, month, day):
#Esta função retorna o dia seguinte ao dia informado na primeira data,
# para que, na função daysBetweenDates(), possa ser acumulado a qtd de dias
# até chegar na segunda data
if day < daysInMonth(year,month):
return year, month, day + 1
else:
if month == 12:
return year + 1, 1, 1
else:
return year, month + 1, 1
def dateIsBefore(year1, month1, day1, year2, month2, day2):
#Verifica se a data1 informada é anterior a data2, caso contrário não há como
# calcular a qtd de dias, pois não temos como contar datas futuras
if year1 < year2:
return True
elif year1 == year2:
if month1 < month2:
return True
if month1 == month2:
return day1 < day2
else:
return False
def daysBetweenDates(year1, month1, day1, year2, month2, day2):
# O assert serve como teste para verifiar se a data1 é menor que a data2, caso não seja o programa para aqui e retorna erro
# assert not dateIsBefore(year2, month2, day2, year1, month1, day1)
days = 0
while dateIsBefore(year1, month1, day1, year2, month2, day2):
year1, month1, day1 = nextDay(year1, month1, day1)
days += 1
return days
def test():
test_cases = [((2012,1,1,2012,2,28), 58),
((2012,1,1,2012,3,1), 60),
((2011,6,30,2012,6,30), 366),
((2011,1,1,2012,8,8), 585 ),
((1900,1,1,1999,12,31), 36523)]
for (args, answer) in test_cases:
result = daysBetweenDates(*args)
if result != answer:
print ("Test with data:", args, "failed")
else:
print ("Test case passed!")
test()