Functions: Intermediate Practice Problems sreen 6/8; why does the solution work?

The solution (below) is for the function that takes in a list of values as an argument, and returns the minimum and maximum value. I understand the first part (creating the frequency table), but I don’t understand why the code for determing the min/max values work. Can someone please explain the logic to me?

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
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))

welcome @chris.maygrow .

I see that this is your first post, and I remember perfectly how lost I was at the beginning, so if you allow me I would recommend that you learn a little markdown,

markdownguide

for example your code would look like this:

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
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))

I don’t know how your code ends precisely because I don’t know where the tab ends

Another thing is that you must put the link to which you are referring. It is very important because if I do not know if the code is well written and neither the context then I can not help you much.

Now you have a sample of how md goes, so upload the link and I will help you.

A&E