Program of the Week 01 - Common Day Finder

I know the title of this post ain't that great and TBH I couldn't come up with something better. So what is this program about. A few days back a friend of mine celebrated his birthday. I was wondering how do I find all the years when, his birthday and mine fall on the same day of the week. It was not a great question, but it piqued me and I wrote a rudimentary program for it on my phone. While this is not the same code I wrote it is kinda similar.

I will be writing it in Python as that is the language I'm most comfortable in right now.

I'll be using the trusty 'datetime' utility in python for this task.

Below is the basic version of the program:


from datetime import datetime

# Initialize the dates
date1 = "15/02"
date2 = "23/05"

# Initialize the start and end years
start_year = 2019
end_year = 2100

# Initialize a list for all results
results = []

# Now loop in the year range and find the days
for year in range(start_year, end_year):

    # Find the days
    day1 = datetime.strptime(date1 + "/" + str(year),'%d/%m/%Y').strftime('%A')
    day2 = datetime.strptime(date2 + "/" + str(year),'%d/%m/%Y').strftime('%A')
    
    if day1 == day2:
        results.append((year,day1))
        
# Print the count
print("{0} common days found".format(len(results)))
# 20 common days found

# Print the days and years
print('\n'.join("{0} \t {1}".format(*res) for res in results))

# 2020   Saturday
# 2024   Thursday
# 2028   Tuesday
# 2032   Sunday
# 2036   Friday
# 2040   Wednesday
# 2044   Monday
# 2048   Saturday
# 2052   Thursday
# 2056   Tuesday
# 2060   Sunday
# 2064   Friday
# 2068   Wednesday
# 2072   Monday
# 2076   Saturday
# 2080   Thursday
# 2084   Tuesday
# 2088   Sunday
# 2092   Friday
# 2096   Wednesday

So what I've done in this piece of code is that first I initialize the two dates that I'm going to use. Next I choose the years between which I'm going to find the common days. Then I loop over the range of the years I have initialized earlier.

In this for loop, I first convert the datetime 'strings' to 'datetime' format using datetime.strptime. Once I have this datetime object I get a string of the day of the week of the selected date using "datetime.strftime('%A')". Here "%A" is the notation for the day of the week of the day. Once I have them I store it in the two variables.

For each year I compare these two variables, if they are same, it means that the dates have the same day of the week. This I store in a list using result.append().

Next I get the count of the days this occurs using the len() method which gives me the length of the list. And then I print out all the years in the last step.

While this is a basic program which can handle two dates, what can we do if we want to handle more dates? I have modified the program a bit and written a code for it below.

# Initializing multiple dates in a list
dates = ['19/02','27/05','24/06']

#We will use the year range from the previous example

complex_result = []

for year in range(start_year, end_year):
    comparision_set = set()
    for date in dates:
        day = datetime.strptime(date + "/" + str(year),'%d/%m/%Y').strftime('%A')
        comparision_set.add(day)
    if len(comparision_set) == 1:
        complex_result.append((year, list(comparision_set)[0]))

# Printing the number of records
print("{0} common days found".format(len(complex_result)))
# 20 common days found

# Printing the records
print('\n'.join("{0} \t {1}".format(*res) for res in complex_result))

# 2020   Wednesday
# 2024   Monday
# 2028   Saturday
# 2032   Thursday
# 2036   Tuesday
# 2040   Sunday
# 2044   Friday
# 2048   Wednesday
# 2052   Monday
# 2056   Saturday
# 2060   Thursday
# 2064   Tuesday
# 2068   Sunday
# 2072   Friday
# 2076   Wednesday
# 2080   Monday
# 2084   Saturday
# 2088   Thursday
# 2092   Tuesday
# 2096   Sunday 

So here we take a list of dates. Then we find out there day of the week over the range of years. Now for comparison I have initialized a set. A set in python doesn't allow duplicate elements.So if the day of week is same for all, the number of elements in the set will be 1. If this condition is true the day and year get added to the results list. Which is then printed.

So this is it for now. Further I wanted to add functionality that can do 2 days, 3 days and so on for more dates in the list. Maybe I'll do that for next week.

If you have any suggestions or ideas for other programming problems, please do post them in the comments. That's all for week one of program of the week. One down fifty one to go. :)

Comments

Post a comment

Popular posts from this blog

My Experiments with Pi (Part 2 of N)

My Experiments with Pi (Part 1 of N)