Exercises Using functions from examples 3, 4 & 5, write functions to: Calculate the difference between two dates in days. Calculate which day in the week corresponds to a certain date given another date and its corresponding day of the week.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
Exercises
Using functions from examples 3, 4 & 5, write functions to:
Calculate the difference between two dates in days.
Calculate which day in the week corresponds to a certain date
given another date and its corresponding day of the week.
Transcribed Image Text:Exercises Using functions from examples 3, 4 & 5, write functions to: Calculate the difference between two dates in days. Calculate which day in the week corresponds to a certain date given another date and its corresponding day of the week.
Example (3)
Write a function which takes 1 argument (a year) and
returns True if year is leap, or False otherwise.
def isYearLeap (year):
if year%400==0: return True
if year%4==0 and year%100!=0: return True
return False
print (isYearLeap (2000))
print (isYearLeap (2016))
# Output: True
# Output: True
# Output: False
# Output: False
print (isYearLeap (1900))
print (isYearLeap (2019))
CSC 202 - BASICS OF PROGRAMMING AND ALGORITHMS
15
Example (4)
>Write a function which takes 2 arguments (year & month)
and returns number of days for the given month/year pair.
def daysInMonth (year, month):
days
= [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if isYearLeap (year) and month==2: return 29
else: return days [month-1]
print (daysInMonth (2000, 2))
print (daysInMonth (2019, 2))
print (daysInMonth (2018, 12))
print(daysInMonth (1900, 4))
# Output: 29
# Output: 28
# Output: 31
# Output: 30
CSC 202 - BASICS OF PROGRAMMING AND ALGORITHMS
16
Example (5)
>Write a function which takes 3 arguments (year, month &
day of month) and returns corresponding day of the year.
def dayOfYear (year, month, day):
days
= day
for i in range (1, month):
days += daysInMonth (year, i)
return days
print (dayofYear (2019, 4, 15))
# Output: 105
Transcribed Image Text:Example (3) Write a function which takes 1 argument (a year) and returns True if year is leap, or False otherwise. def isYearLeap (year): if year%400==0: return True if year%4==0 and year%100!=0: return True return False print (isYearLeap (2000)) print (isYearLeap (2016)) # Output: True # Output: True # Output: False # Output: False print (isYearLeap (1900)) print (isYearLeap (2019)) CSC 202 - BASICS OF PROGRAMMING AND ALGORITHMS 15 Example (4) >Write a function which takes 2 arguments (year & month) and returns number of days for the given month/year pair. def daysInMonth (year, month): days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if isYearLeap (year) and month==2: return 29 else: return days [month-1] print (daysInMonth (2000, 2)) print (daysInMonth (2019, 2)) print (daysInMonth (2018, 12)) print(daysInMonth (1900, 4)) # Output: 29 # Output: 28 # Output: 31 # Output: 30 CSC 202 - BASICS OF PROGRAMMING AND ALGORITHMS 16 Example (5) >Write a function which takes 3 arguments (year, month & day of month) and returns corresponding day of the year. def dayOfYear (year, month, day): days = day for i in range (1, month): days += daysInMonth (year, i) return days print (dayofYear (2019, 4, 15)) # Output: 105
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY