Screen Link:
https://app.dataquest.io/m/1013/functions%3A-intermediate-practice-problems/6/most-frequent-and-least-frequent
My Code:
values = [10, 20, 30, 10, 30, 10]
# answer to this input: 3, 1
def most_least_frequent(values):
freq = {}
for number in values:
if number in freq:
freq[number] +=1
else:
freq[number] = 1
maximum = 0
minimum = 1
for i in freq:
if freq[i] > maximum:
maximum = freq[i]
if freq[i] <= minimum:
minimum = freq[i]
return maximum, minimum
print(most_least_frequent(values))
Replace this line with your code
What I expected to happen:
when i run the code It is showing me right output which is (3,1) but it is showing me an error saying
What actually happened:
“Your function returned an incorrect answer. Input: [[‘1’, ‘1’, ‘1’, ‘1’, ‘1’, ‘1’, ‘1’]] Your answer: 7, 1 Expected answer: 7, 7”
can you please help me what is wrong in my code?
Also i did not understand why solution uses the None for Maximum and minimum value.
Here is the solution:
# provided input
values = [10, 20, 30, 10, 30, 10]
# answer to this input: 3, 1
def most_least_frequent(values):
# 1. compute the frequencies
freq = {}
for v in values:
if v in freq:
freq[v] += 1
else:
freq[v] = 1
# 2. compute the minimum and maximum
max_freq = None **I don't understand why to use None instead of 0**
min_freq = None
for v in freq:
if max_freq is None or freq[v] > max_freq:
max_freq = freq[v]
if min_freq is None or freq[v] < min_freq:
min_freq = freq[v]
return max_freq, min_freq
print(most_least_frequent(values))