alt text

The Calendar Problem

alt text

In [1]:
from IPython.display import YouTubeVideo
YouTubeVideo('pdMUk16675U')
Out[1]:

Determine what day of the week YOUR BIRTHDAY will fall on this year.

For example, January 1, 2019 was Tuesday, and January 1, 2020 will be Wednesday.

Before you look at the code below, ask yourself what information you need to solve this challenging problem.

See if you can develop an "algorithm" where you input the month and day (e.g. 6 and 15 if your birthday is June 15), and your algorithm correctly outputs whether your birthday will fall on a Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, or Sunday.

In [2]:
def calculate_day(month,day): 
    
    if month==1: shift=0
    elif month==2: shift=3
    elif month==3: shift=3
    elif month==4: shift=6
    elif month==5: shift=1
    elif month==6: shift=4
    elif month==7: shift=6
    elif month==8: shift=2
    elif month==9: shift=5
    elif month==10: shift=0
    elif month==11: shift=3
    elif month==12: shift=5
    else: return "ERROR" 
        
    mytotal = day + shift
        
    remainder = mytotal % 7
    
    if remainder==0: return "Monday"
    elif remainder==1: return "Tuesday"
    elif remainder==2: return "Wednesday"
    elif remainder==3: return "Thursday"
    elif remainder==4: return "Friday"
    elif remainder==5: return "Saturday"
    elif remainder==6: return "Sunday"
    else: return "ERROR"
In [3]:
calculate_day(6,15)
Out[3]:
'Saturday'

Double-check your answer using this handy widget. After you hit the "Run" button, you'll be able to use the sliders to change the month and day to your birthday.

Note: this widget is for the year 2019. For the year 2020, add one day (e.g. Jan 1, 2019 is a Tuesday, and so Jan 1, 2020 is a Wednesday)

In [4]:
from __future__ import print_function
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets

interact(calculate_day, month=widgets.IntSlider(min=1, max=12, step=1, value=6), day=widgets.IntSlider(min=1, max=31, step=1, value=15))
Out[4]:
<function __main__.calculate_day(month, day)>

alt text