같은 해의 두 날짜 사이 간격

 

글. 오상문 sualchi@daum.net

 

1월 1일부터 지난 날짜를 각각 구하여 뺄셈을 한다.

각 달의 날짜 수는 다음과 같다고 가정한다.

31,28,31,30,31,30,31,31,30,31,30,31

 

#--------------------------------------------

# 1월1일에서 특정 날짜의 간격 돌려줌 ( solution1 함수에서 사용 )

def daySum(mon, day):  
  monList = [0,31,28,31,30,31,30,31,31,30,31,30,31]
  return sum(monList[1:mon]) + day - 1

 

def solution1(startMonth, startDay, endMonth, endDay):
  startTotal = daySum(startMonth, startDay)
  endTotal = daySum(endMonth, endDay)
  return endTotal - startTotal
#-----------------------------------------------

 

# solution2 함수는 직접 간격을 구하여 계산함

def solution2(startMonth, startDay, endMonth, endDay):
  mList = [0,31,28,31,30,31,30,31,31,30,31,30,31]
  startTotal = sum(mList[1:startMonth]) + startDay - 1
  endTotal = sum(mList[1:endMonth]) + endDay - 1 
  return endTotal - startTotal

#---------------------------------------------------


#테스트
start_mon = 1
start_day = 2
end_mon = 2
end_day = 2

 

ret1 = solution1(start_mon, start_day, end_mon, end_day)
ret2 = solution2(start_mon, start_day, end_mon, end_day)

 

print("solution1 함수 반환값", ret1)  # 31
print("solution2 함수 반환값", ret2)  # 31


<이상>

반응형

+ Recent posts