USING LISTS IN DICTIONARY :-
Problem :
Confusing, yes it will confuse you.Actually if you want to hold a list in the key of a dictionary,what would you do.problem seems quite interesting na. Actually it's not. Yeah just adding a line in your source will resolve this issue.
Explanation :
Usually python dictionaries allows you to store key value pairs. You can't store a list in the corresponding key if you use the normal dictionary. It will raise an exception. So just add a line in your source.what's that line...???
It's simple.Are you aware of the defaultdict subclass of the dict type...???If not,just know it here.Everything about defaultdict is clear now,let's hope it is.We can utilize this one to make the dictionaries to hold the list. just add defaultdict(list) and everything is set now.just use it in the program like below.
SOURCE CODE : IDEONE
__AUTHOR__ = "pvkcse"
__STUDENTAT__ = "Accet,Kkdi"
__ALGO__ = "Counting Sort"
try:
from collections import defaultdict
import sys
except ImportError :
sys.stderr.write("Error In Importing The Modules...\n")
pass
def __sort(lists,key=lambda x : int(x)) :
answer,freq_list=[],defaultdict(list)
for i in lists :
freq_list[key(i)].append(i)
for i in range(min(freq_list),max(freq_list)+1) :
answer.extend(freq_list[i])
return answer
def main(*args,**kwargs) :
try :
lists=list(map(int,sys.stdin.readline().split(' ')))
except ValueError:
sys.stderr.write("Enter valid number...\n")
try :
print " ".join(str(__sort(lists)[i]) for i in range(len(lists)))
except :
sys.stderr.write("Error in writing the data...\n")
if __name__ == "__main__" :
main()
This is the simple program for implementing the counting sort in python.
If you find anything wrong in the implementation,then just notify it in the comment box .
22:37
Unknown
Posted in
0 comments :
Post a Comment