Answer to Question #184743 in Python for abhishek

Question #184743

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

Sample Input 1

25 Jan 2021

14 Feb 2021

Sample Output 1

Saturday: 3

Sunday: 3




1
Expert's answer
2021-04-24T06:11:36-0400
from datetime import  date


MON=0
TUE=1
WED=2
THR=3
FRI=4
SAT=5
SUN=6


Date_1 = input("Enter Start Date in (DD:MM:YYYY) format: ")
Date_2 = input("Enter End   Date in (DD:MM:YYYY) format: ")


year_1 = int(str(Date_1[6]+Date_1[7]+Date_1[8]+Date_1[9]))
year_2 = int(str(Date_2[6]+Date_2[7]+Date_2[8]+Date_1[9]))


m_1 = int(str(Date_1[3]+Date_1[4]))
m_2 = int(str(Date_2[3]+Date_2[4]))


day_1 = int(str(Date_1[0] + Date_1[1]))
day_2 = int(str(Date_2[0] + Date_2[1]))


d1 = date(year_1, m_1, day_1)
d2 = date(year_2, m_2, day_2)


count_SAT = 0
count_SUN = 0


print("\n")
for d_ord in range(d1.toordinal(), d2.toordinal()):
    d = date.fromordinal(d_ord)
    if (d.weekday() == SAT):
        print("Date: ",d,"\tis a Saturday")
        count_SAT += 1
    if (d.weekday() == SUN):
        print("Date: ",d,"\tis a Sunday")
        count_SUN += 1


count = count_SAT + count_SUN
#print("\nTotal no. of Saturdays from %2d-%2d-%2d to %2d-%2d-%4d = %d"%(day_1,m_1,year_1,day_2,m_2,year_2,count_SAT))
#print("Total no. of Sundays   from %2d-%2d-%2d to %2d-%2d-%4d = %d"%(day_1,m_1,year_1,day_2,m_2,year_2,count_SUN))


print("\nSaturdays: %d"%count_SAT)
print("\nSundays  : %d"%count_SUN)


Python Output:





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