Sunday, February 25, 2018

Python program : Lists

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]) 
 
def frequency(l):
  m=sorted(l)
  d={x:m.count(x) for x in m}
  hfelindex=[]
  lfelindex=[]
  hfel=[]
  lfel=[]
  k=list(d.keys())
  v=list(d.values())
  h=max(v)
  g=min(v)
  for i in range(0,len(v)):
    if v[i] == h:
	    hfelindex += [i]
  for j in hfelindex:
    hfel += [k[j]]
  p=sorted(hfel)
  for a in range(0,len(v)):
    if v[a] == g:
	    lfelindex += [a]
  for b in lfelindex:
    lfel += [k[b]]
  q=sorted(lfel)
  return((q,p))
 
Thanks
Happy programming ! 
 
 
 

No comments:

Post a Comment

How can I run a C++ program directly from Windows?

How-can-I-run-a-C-program-directly-from-Windows