Answer to Question #199890 in Python for meghana

Question #199890

Given two dates D1 and D2, write a program to count the number of Saturdays and Sundays from D1 to D2 (including D1 and D2).

The date in string format is like "8 Feb 2021".Input


The first line of input will contain date D1 in the string format.

The second line of input will contain date D2 in the string format.Output


The output should be a single line containing two integers separated by space.Explanation


For example, if the given dates are "25 Jan 2021" and "14 Feb 2021", the Saturdays and Sundays dates from "25 Jan 2021" to "14 Feb 2021" are

"30 Jan 2021" is a Saturday

"31 Jan 2021" is a Sunday

"6 Feb 2021" is a Saturday

"7 Feb 2021" is a Sunday

"13 Feb 2021" is a Saturday

"14 Feb 2021" is a Sunday

So the output should be

Saturday: 3

Sunday: 3




1
Expert's answer
2021-05-30T07:18:16-0400
import datetime as tm

print("Enter dates like: 8 Feb 2021")

start     = tm.datetime.strptime(input('Start date:'), '%d %b %Y')
end       = tm.datetime.strptime(input('End date:'), '%d %b %Y')

day       = tm.timedelta(days=1)

saturdays = 0
sundays   = 0

while start <= end:
    if start.isoweekday() == 6:
        saturdays += 1
    if start.isoweekday() == 7:
        sundays   += 1
    start += day

print('satudays:', saturdays)
print('sundays:', sundays)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS