# Does anyone have the solution for who visited the POTUS most each month?

I have been trying to solve his exercise but I am stuck and getting nowhere. Does anyone have solution for this?

Thanks,
Mahesh

Hi Mahesh,

In this discussion you will find a lot of ways to solve it.

Hi Elena: I tried that but did not succeed. I almost got drained in that for quite some time:-(

I have just tried the very first code from that discussion, and it worked perfectly! I’m sure that you probably had some confusion with the indendation of that code, espesially with the `return` statement Because it’s a typical issue.

This is the correct indentation:

``````def most_freq_visitor_that_month(month):
visitor_freq = {}
for row in potus:
visitor_name = row[0]
start_date = row[2]
appt_month = start_date.month
if appt_month == month:
if visitor_name in visitor_freq:
visitor_freq[visitor_name] += 1
else:
visitor_freq[visitor_name] = 1
return max(visitor_freq)

month_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

for mo in month_list:
print(mo, ":", most_freq_visitor_that_month(mo))
``````

Output:

``````1 : laura R. Marquez
2 : therese n. mcmillan
3 : terry a. henry
4 : vincent m. morris
5 : zoe p. parker
6 : zain i. boxer
7 : young John Hardin
8 : rebecca l. christopher
9 : william G. Davis
10 : zachary clomera
11 : nancy n. games
12 : yena n. Bae
``````
2 Likes

I did not attempt to this method because in the comments they said it is not the best, so I am trying with default dict and nested dicts but I think I am doing totally wrong. Just trying to see if there is a correct way to do this.

mo = {“January”:{},
“February”:{},
“March”:{},
“April”:{},
“May”:{},
“June”:{},
“July”:{},
“August”:{},
“September”:{},
“October”:{},
“November”:{},
“December”:{}
}

month_dict = defaultdict(int)
for each in potus:
name = each[0]
date = each[2]
month = date.month
month = date.strftime("%B")
if month in mo:
if name not in month_dict:
month_dict[name] = 1
else:
month_dict[name] = 1
mo[month] = month_dict

Well, it’s not the most elegant method, I agree, but at least it works. The best approach from there seems to be the one using pandas library, but probably you haven’t learned pandas yet.

would you say to move forward with this to next as we may never use this and always use pandas?

I think a good idea is to resolve it now by using dictionaries and then later, after learning pandas, return to this excercise and resolve it in the light of the new knowledge. And then compare these 2 approaches, and also cross-check the output results.

mo = {“January”:{},
“February”:{},
“March”:{},
“April”:{},
“May”:{},
“June”:{},
“July”:{},
“August”:{},
“September”:{},
“October”:{},
“November”:{},
“December”:{}
}

month_dict = defaultdict(int)
for each in potus:
name = each[0]
date = each[2]
month = date.month
month = date.strftime("%B")
if month in mo:
if name not in mo[month]:
mo[month][name] = 1
else:
mo[month][name] += 1

I was able to get the result but just cant figure out how to get maximum for each month

Hooray ! I finally got it. And I got the result correctly. I validated manually with the POTUS CSV files and answers match. However the other discussion values are incorrect. You can check it out in POTUS csv the count does not match. For ex: January Jesus MurilloKaram
visisted 3 times but the other discussion result was laura marquez.
In februray there are 2 visitors (Keith Ellison, Sheila JacksonLee) with same count but max function returned only one. Validated with the csv file manually and is correct. This is different story but I feel better now. please try this code and let me know if you get same.

mo = {“January”:{},
“February”:{},
“March”:{},
“April”:{},
“May”:{},
“June”:{},
“July”:{},
“August”:{},
“September”:{},
“October”:{},
“November”:{},
“December”:{}
}

for each in potus:
name = each[0]
date = each[2]
month = date.month
month = date.strftime("%B")
if month in mo:
if name not in mo[month]:
mo[month][name] = 1
else:
mo[month][name] += 1

for each in mo:
month_dict1 = mo[each]
m = max(month_dict1, key=month_dict1.get)
print(each, ‘:’, m)

Output:
January : Jesus MurilloKaram
February : Sheila JacksonLee
March : Anna M. Yeo
April : Alan C. Prather
May : Chris Coons
June : Marilda W. Averbug
July : Russell A. Wilson
August : ELIZABETH C. NUNEZ
September : Shaojun ■■■■
October : AnnaMaria R. Mottola
November : MICHAEL LISZEWSKI
December : Brian E. Fallon

3 Likes

That’s great Mahesh, I’ve tried your code now and it works! You are right then, the output of the previous code was not correct, so you did well keeping trying.

Next time, however, when you put some code here, please surround them with backticks (they are close to the `Esc` on the keyboard, I didn’t know about their existence before DQ! ). For a small one-line piece of code use one backtick at the beginning and one at the end. For a several-lines code - three backticks at the beginning and three at the end, without white spaces. Then it will be much easier to read your code, and also all the indentations will be preserved.

1 Like

Noted, Thanks Elena.