get_earliest

Python Morsels Exercise

Return the earliest MM/DD/YYYY date string given

1 bonuses 8 hints 10 solutions 971 users solved 75 reviews

This exercise is a somewhat silly one. I'd like you to compare the date strings, but allow invalid dates while comparing them.

I want you to write a function that takes two strings representing dates and returns the string that represents the earliest point in time. The strings are in the US-specific MM/DD/YYYY format... just to make things harder. Note that the month, year, and day will always be represented by 2, 4, and 2 digits respectively.

Your function should work like this:

>>> get_earliest("01/27/1832", "01/27/1756")
'01/27/1756'
>>> get_earliest("02/29/1972", "12/21/1946")
'12/21/1946'
>>> get_earliest("02/24/1946", "03/21/1946")
'02/24/1946'
>>> get_earliest("06/21/1958", "06/24/1958")
'06/21/1958'

There's a catch though. Your exercise should work with invalid month and date combinations. What I mean by that is that dates like 02/40/2006 should be supported. By that I mean 02/40/2006 is before 03/01/2006 but after 02/30/2006 (dates don't roll over at all). I'm adding this strangely arbitrary requirement just so you can't rely on Python's datetime module.

There are many ways to solve this. See if you can figure out the clearest and most idiomatic way to solve this exercise.

Bonus 1


This is just a preview of the problem statement.

This exercise includes 1 bonus and 8 hint links.

To solve this exercise, sign in to your Python Morsels account.

A learning habit for experienced Pythonistas

Profile picture of Trey

My name is Trey Hunner and I hold Python trainings for teams. I've spent countless hours refining my most interesting Python exercises into Python Morsels, a Python skill-building platform for folks who already know Python. Python Morsels is design to help you deepen your Python skills every week.

Sign up for Python Morsels to access this exercise and many more.

Join Python Morsels ✨