나는 수색과 동일한 문제를 가진 사람은 여기에서 찾을 발견 : Python daysBetweenDate무한 루프는 날짜 카운터 동안
그러나, 자신의 코드 내 다른이며, 내가 지적 된 문제를 해결하기 위해 나타납니다 그 게시물에 대한 답변.
제 코드에서는 무한 루프가 발생하는 이유를 알 수 없습니다.
def leap_year_check(year):
if (year%4==0 and year%100==0 and year%400==0) or (year%4==0 and year%100!=0):
return True
else:
return False
#TESTED-leap_year_check-PASSED
def nextDay(year, month, day):
if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12 and day < 31:
return year, month, day + 1
if month == 4 or month == 6 or month == 9 or month == 11 and day < 30:
return year, month, day + 1
if month == 2 and day <28:
return year, month, day + 1
if month == 2 and leap_year_check(year)==True and day==28:
return year, month, day + 1
else:
if month == 12:
return year + 1, 1, 1
else:
return year, month + 1, 1
#TESTED nextDay - PASSED
def dateIsBefore(year1, month1, day1, year2, month2, day2):
if year1 < year2:
return True
if year1 == year2:
if month1 < month2:
return True
if month1 == month2:
return day1 < day2
return False
#TESTED dateIsBefore - PASSED
def daysBetweenDates(year1, month1, day1, year2, month2, day2):
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
#***Keep running into infinite loop when testing daysBetweenDates
#ALL other functions operate correctly when tested on their own
#ALL other functions work together EXCEPT when running daysBetweenDates
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()
지금까지 내가, daysBetweenDates가 nextDay 기능과 단위를 실행하는 이해할 수있는 일 +1, 한 dateIsBefore 다음 dateIsBefore이 True가 될 때 사이의 일 수를 추가해야하는, False를 반환으로 .
나는 분명히 뭔가가 누락되었습니다.
나는 이것이 매우 비효율적 일 가능성이 높지만 학습 목적을위한 것이며 아직 최적화에 집중해야한다는 점을 분명히 알고 있습니다. 지금은 코드를 작동시키는 법을 이해하려고 노력하고 있습니다.
도움을 주시면 감사하겠습니다.