Write a Python function that takes as input a list of integers returns the most occuring and least occuring integers. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Write a Python function that takes as input a list of integers returns the most occuring and least occuring integers.

For instance>>> frequency([13,12,11,13,14,13,7,11,13,14,12]) Should return ([7], [13]) >>>frequency([13,12,11,13,14,13,7,11,13,14,12,14,14]) Should return ([7], [13, 14])

28th Aug 2019, 5:28 PM
Sourav A.S
16 Answers
+ 1
show your code to see that you atleast tried
28th Aug 2019, 5:31 PM
Cat Sauce
Cat Sauce - avatar
+ 1
sounds like homework :)
28th Aug 2019, 5:33 PM
Brave Tea
Brave Tea - avatar
+ 1
😅
28th Aug 2019, 5:35 PM
Sourav A.S
+ 1
Sourav A.S sorry my brother broke his leg, but hes alright now il check when i wake up
28th Aug 2019, 9:06 PM
Cat Sauce
Cat Sauce - avatar
+ 1
Cat Sauce geez, thats something else. good luck to you and your brother. happy to hear he’s is sort of alright now.
29th Aug 2019, 12:44 AM
Brave Tea
Brave Tea - avatar
+ 1
Cat Sauce Thanks alot. I hope your brother is ok.
29th Aug 2019, 2:44 AM
Sourav A.S
0
Sourav A.S if you can show us a try you made but failed, il be happy to help you :)
28th Aug 2019, 5:38 PM
Cat Sauce
Cat Sauce - avatar
0
def frequency (i): d=[ ] d.append(( max(set(i), key=i.count))) print(d)
28th Aug 2019, 5:49 PM
Sourav A.S
0
It is only for getting most occuring integer, but it doesnt Work if there are multiple integers occuring most
28th Aug 2019, 5:50 PM
Sourav A.S
0
so you want to check how many times all the integers have occured?
28th Aug 2019, 5:54 PM
Cat Sauce
Cat Sauce - avatar
0
I want know which integers are leat occuring and which are most occuring
28th Aug 2019, 5:55 PM
Sourav A.S
0
For eg: Fun([1,2,1,3,2]) Should return [3,[1,2]] Where 3 is the least occuring and 1,2 are the most occuring
28th Aug 2019, 5:58 PM
Sourav A.S
0
wait 1 min and il help
28th Aug 2019, 6:00 PM
Cat Sauce
Cat Sauce - avatar
0
Cat Sauce did you get the code
28th Aug 2019, 6:49 PM
Sourav A.S
0
I'm sure I've seen something similar been done on here before, Look up Counter in collections module in python.org.
28th Aug 2019, 7:12 PM
rodwynnejones
rodwynnejones - avatar
0
I think this does exactly what you need. from collections import Counter mylist = [15, 20, 33, 4, 15, 20, 4, 20, 2, 3, 4] mylist2 = [] for x in mylist: mylist2.append(str(x)) # had to do the (str), something about the Count function needing hashable. def get_most_least(x): most_occuring = [] least_occuring = [] combined_list = [] cnt = Counter(x) for a, b in cnt.items(): if b == max(cnt.values()): most_occuring.append(a) if b == min(cnt.values()): least_occuring.append(a) combined_list = [least_occuring, most_occuring] return combined_list print(get_most_least(mylist2))
28th Aug 2019, 10:27 PM
rodwynnejones
rodwynnejones - avatar