Click here to Skip to main content
15,912,977 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a Python function frequency(l) that takes as input a list of integers and returns a pair of the form (minfreqlist,maxfreqlist) where
minfreqlist is a list of numbers with minimum frequency in l, sorted in ascending order
maxfreqlist is a list of numbers with maximum frequency in l, sorted in ascending
For instance
>>> frequency([13,12,11,13,14,13,7,11,13,14,12])
([7], [13])

>>> frequency([13,12,11,13,14,13,7,11,13,14,12,14,14])
([7], [13, 14])

>>> frequency([13,12,11,13,14,13,7,11,13,14,12,14,14,7])
([7, 11, 12], [13, 14])

What I have tried:

I have tried making a program for it...which din't work properly...please help
Posted
Updated 20-Aug-17 20:46pm
Comments
Richard MacCutchan 19-Aug-17 7:44am    
Show the code you have tried and explain exactly what the problem is.

Start by counting the number of occurrences of each value. You also need some way to store this information, so look at the collection types that can keep two values together in this way: maybe a dictionary. Once you have that information you can find the minimum and maximum occurrences. And once you have those two values you can go through the collection making a list of all the values with minimum count, and all those with maximum.
 
Share this answer
 
I am not going to tell you the solution But I can give you a hint.

** Turn that list into a key-value pair. Where key is the element in the list and value is the count of the elements.

This works as you can make a unique dictionary containing just the elements and their corresponding frequency.
 
Share this answer
 
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.
So if you've tried, you have code with maybe a small problem - for that you need the debugger.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
<KickAss mode>
Take a sheet of paper and a pencil. Use your brain and solve the problem by hand. Since Harry Potter didn't gave you the solution, there is no magic, and the solution didn't jump to your face.
You solved the problem by following a procedure, that procedure is your algorithm. You need to write down the steps in a mechanical manner (computer like). The program will follow those steps.
</KickAss mode>

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.

The idea of "development" is as the word suggests: "The systematic use of scientific and technical knowledge to meet specific objectives or requirements." BusinessDictionary.com[^]
That's not the same thing as "have a quick google and give up if I can't find exactly the right code".
Quote:
I have tried making a program for it...which din't work properly

Show your code to get help on it.
 
Share this answer
 
v3
Python
import math
def frequency(l):
  a = []
  a = l
  b = []
  c = []
  k = ()
  minfreqlist = []
  maxfreqlist = []
  if len(l) == 0:
    k = ([],[])
    return k
  if l.count(l[0]) == len(l):
    k = ([l[0]],[l[0]])
    return k
  a.sort()
  var = a[0]
  b.append(var)
  for i in range(len(a)):
    if a[i]!= var:
      var = a[i]
      b.append(var)
  for i in range(len(b)):
    c.append(a.count(b[i]))
  min_ = min(c)
  max_ = max(c)
  for i in range(len(c)):
    if c[i] == min_:
      minfreqlist.append(b[i])
    elif c[i] == max_:
      maxfreqlist.append(b[i])
  k = (minfreqlist,maxfreqlist)
  return k
 
Share this answer
 
v2
Comments
Richard MacCutchan 21-Aug-17 4:12am    
Please do not do people's homework for them; it is not helpful.
def frequency(l):
result = {}
for i in l:
result[i] = l.count(i)

return ([min(result.keys())],[max(result.keys())])
 
Share this answer
 
def frequency(list):
  x = {}
  minlist = []
  maxlist = []
  
  for i in list:
    x[i] = x.get(i,0) + 1
    
  min_val = min(x.values())
  for key in x.keys():
    if x[key] == min_val:
      minlist = key
        
  max_val = max(x.values())
  for key in x.keys():
    if x[key] == max_val:
      maxlist = key
      
  
  return ([minlist],[maxlist])
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900