Skip to content
Advertisement

How to find the most common day name in a month?

Is there a way to print the day of the week which appears most frequent in the specified month and year as a string. Like Monday and Tuesday and if there are any multiple days, the first one should be displayed.

First I tried to check with the datetime output but unable to find the calendar module to get this as day output.

JavaScript

I was trying to use calendar.itermonthdays to get the required iterator to use that to display days like ‘Monday’, Tuesday’. But only came up to this

JavaScript

Advertisement

Answer

You can use collections.Counter for this. You can call the calendar’s itermonthdates for a given year and month, then create a Counter of the weekday names using the '%A' format specifier of datetime.

JavaScript

Then for example

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement