'Need help in finding the day of given (year,month,date) in python
def isYearLeap(year): # Leap year formula
if year % 4 == 0 and year % 100 != 0 :
return True
elif year % 400 == 0 :
return True
else :
return False
testData = [1900, 2000, 2016, 1987] # Test Data as reference
testResults = [False, True, True, False]
for i in range(len(testData)):
yr = testData[i]
print(yr,"->",end="")
result = isYearLeap(yr)
if result == testResults[i]:
print("OK")
else:
print("Failed")
def daysInMonth(year, month): # Finding out days in months in common & leap year
days = 0
mo31 = [1,3,7,5,8,10,12]
if year % 4 == 0 :
days = 1
if year % 100 == 0 :
days = 0
if year % 400 == 0 :
days = 1
if month == 2 :
return 28 + days
if month in mo31 :
return 31
return 30
testYears = [1900, 2000, 2016, 1987] # Test Data as reference
testMonths = [2, 2, 1, 11]
testResults = [28, 29, 31, 30]
for i in range(len(testYears)):
yr = testYears[i]
mo = testMonths[i]
print(yr, mo, "->", end="")
result = daysInMonth(yr, mo)
if result == testResults[i]:
print("OK")
else:
print("Failed")
def dayOfYear(year, month, day):
doy = ["Sat","Sun","Mon","Tue","Wed","Thu","Fri"] # Days of week
monthvalue = [1,4,4,0,2,5,0,3,6,1,4,6] # Months value zellers rule
century = [1700,1800,1900,2000] # Zellers rule
value = [4,2,0,6] # Zellers rule
rem = 0 # Remainder Variable
r = [] # Empty List to compare remainder and to a doy
y = str(year) # Converting year into string
y = int(y[2:4]) # Taking last two digits of string & if used return the function ends
y = int(year) # here returning last two digits
m = int(month)
d = int(day)
mo = [] # Empty list for comparing month with monthly values
dd = 0
if dd == 0 :
for i in range(len(monthvalue)) :
mo.append(i) # Creating mo list
if m in mo:
mo[i] == monthvalue[i]
dd = y // 4 + d + m
if m >= 2 :
dd -= 1
dd += y
for i in range(len(value)) :
if y in century :
y = century[i] == value[i]
dd += y
rem = dd % 7
for i in range(len(doy)) :
r.append(i) # Creating r list
if rem in r :
rem = r[i] == doy[i]
print(rem)
print(dayOfYear(2000, 12, 31)) # Giving output False "\n None
Solution 1:[1]
I recognize this as a lab in the netacad cisco python course. You were tasked with creating an is_year_leap() function in LAB 4.3.1.6, a days_in_month() function in LAB 4.3.1.7, and a day_of_year() function in LAB 4.3.1.8. The functions have one, two, three parameters respectively. Please see below code. Note that I excluded a code to return None if any of the arguments are invalid. I would like you to add that on your own as learning.
# from LAB 4.3.1.6
def is_year_leap(year):
if year % 4 == 0: # leap year occur every 4 years & is divisible by 4
return True
else:
return False
# from LAB 4.3.1.7
def days_in_month(year, month):
# Create list with month days for each month as advised in the LAB
month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if is_year_leap(year):
month_days[1] = 29 # 29 days for February on a leap year.
return month_days[month - 1] # month 1 corresponds with index 0 & so on.
# for LAB 4.3.1.8
def day_of_year(year, month, day):
total = 0 # initializing the total variable to add results
# create loop to add only days in the months before the month in the input/test data
for i in range(1, month):
result = days_in_month(year, i)
total += result
day_num = total + day # add the value of the day argument to get day of the year
return day_num
# test data
print(day_of_year(2000, 12, 31))
print(day_of_year(1999, 12, 31))
print(day_of_year(2021, 7, 29))
Solution 2:[2]
A point to note is the occurrence of days above the number of days of months.
def is_year_leap(year):
return year % 4 == 0 and not year % 100 == 0 or year % 400 == 0
def days_in_month(year, month):
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if is_year_leap(year):
days[1] = 29
return days[month - 1]
def day_of_year(year, month, day):
if day <= days_in_month(year, month):
count = 0
for yr in range(1, month):
count += days_in_month(year, yr)
return count + day
Solution 3:[3]
The leap year should fulfil all its conditions for getting the day of the year.
- if the year is divisible by 400 then is_leap_year,
- else if the year is divisible by 100 then not_leap_year
- else if the year is divisible by 4 then is_leap_year
- else not_leap_year
The number of days should also be checked whether it exceeds the day in the month.
#Check for leap year
def is_year_leap(year):
if year%400==0 or (year%100 != 0 and year%4==0):
return True
else:
return False
#Check for number of days in month
def days_in_month(year, month):
month31=[1,3,5,7,8,10,12]
if month in month31:
return 31
elif(month == 2):
if(is_year_leap(year)):
return 29
else:
return 28
else:
return 30
#Check for corresponding day of the year
def day_of_year(year, month, day):
total = 0
if day <= days_in_month(year, month):
total = day
for i in range(month-1):
total += days_in_month(year, i)
return total
else:
return "Your month doesn't have that many days."
print(day_of_year(2000, 12, 31))
print(day_of_year(2001, 12, 31))
print(day_of_year(2008, 12, 31))
print(day_of_year(2000, 11, 31))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chukwunonso Ezeiru |
| Solution 2 | |
| Solution 3 | Gyanchith Hari |
