Practice Python

Beginner Python exercises

19 March 2017

Birthday Months Solutions

Exercise 35

This exercise is Part 3 of 4 of the birthday data exercise series. The other exercises are: Part 1, Part 2, and Part 4.

In the previous exercise we saved information about famous scientists’ names and birthdays to disk. In this exercise, load that JSON file from disk, extract the months of all the birthdays, and count how many scientists have a birthday in each month.

Your program should output something like:

	"May": 3,
	"November": 2,
	"December": 1

Reader solution

Here is one reader solution using built-in Counters. This reader’s solution did not specify what kind of format the months have to be in, but as you can see from their use of x.split()[0] to extract the months that they expect the month format to be of the type “May 5, 2010”.

My solution

In the previous exercise, I saved birthday information in a slightly different format, so I had to use a slightly different method to extract the months. In my dictionary, the birthdays were saved in the format “MM/DD/YYYY”, or the standard date format we use in the US. My birthdays.json file looked like this:

	"Albert Einstein": "03/14/1879",
	"Ada Byron Lovelace": "12/10/1815",
	"Benjamin Franklin": "01/17/1706"

To count how many birthdays are in each month, what we need to do is:

  1. Extract the number that represents the month
  2. Convert that number to a string
  3. Print the result

Here is my code to do that:

import json
from collections import Counter

with open("birthdays.json", "r") as f:
	birthdays = json.load(f)

num_to_string = {
	1: "January",
	2: "February",
	3: "March", 
	4: "April",
	5: "May",
	6: "June",
	7: "July",
	8: "August",
	9: "September",
	10: "October",
	11: "November",
	12: "December"

months = []
for name, birthday_string in birthdays.items():
	month = int(birthday_string.split("/")[0])


And the output you will see is:

Counter({'January': 1, 'March': 1, 'December': 1})

Happy coding!

Share the fun!

comments powered by Disqus