Compare commits
3
Commits
f73731350e
...
752b04b4fe
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
752b04b4fe | ||
|
|
f35db81e06 | ||
|
|
722a209308 |
@@ -0,0 +1,31 @@
|
||||
# from lab23 import is_year_leap
|
||||
|
||||
def is_year_leap(year):
|
||||
if year % 4 == 0:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def days_in_month(year, month):
|
||||
|
||||
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
||||
|
||||
if is_year_leap(year) == True:
|
||||
days[1] = 29
|
||||
|
||||
return days[month - 1]
|
||||
|
||||
test_years = [1901, 2000, 2016, 1987]
|
||||
test_months = [2, 2, 1, 11]
|
||||
test_results = [28, 29, 31, 30]
|
||||
|
||||
for i in range(len(test_years)):
|
||||
yr = test_years[i]
|
||||
mo = test_months[i]
|
||||
|
||||
print(yr, mo, "->", end="")
|
||||
result = days_in_month(yr, mo)
|
||||
if result == test_results[i]:
|
||||
print("OK")
|
||||
else:
|
||||
print("Failed")
|
||||
@@ -0,0 +1,35 @@
|
||||
def is_year_leap(year):
|
||||
if year % 4 == 0:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def days_in_month(year, month):
|
||||
|
||||
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
||||
|
||||
if is_year_leap(year) == True:
|
||||
days[1] = 29
|
||||
|
||||
return days[month - 1]
|
||||
|
||||
def day_of_year(year, month, day):
|
||||
if month < 1 or month > 12:
|
||||
return None
|
||||
elif day < 1 or day > 31:
|
||||
return None
|
||||
elif day > 29 and is_year_leap(year) == True and month == 2:
|
||||
return None
|
||||
elif day > 28 and is_year_leap(year) == False and month == 2:
|
||||
return None
|
||||
|
||||
day_ = day
|
||||
|
||||
for i in range(1,month):
|
||||
mo = days_in_month(year,i)
|
||||
day_ += mo
|
||||
|
||||
return day_
|
||||
|
||||
print(day_of_year(2001,2,29))
|
||||
|
||||
Reference in New Issue
Block a user