Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Denis McMahon Newsgroups: comp.lang.python Subject: Re: Help needed Date: Sun, 30 Nov 2014 12:28:55 +0000 (UTC) Organization: A noiseless patient Spider Lines: 46 Message-ID: References: <7d92e814-9d27-4f7d-a189-49fa84e51a34@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Sun, 30 Nov 2014 12:28:55 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="66ffcfa4470a58bcddbdcd1913f98ab4"; logging-data="13921"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+rXXISAHxFQTgKrRVdVIl5HpUsOXQxGuI=" User-Agent: Pan/0.136 (I'm far too busy being delicious; GIT 926a150 git://git.gnome.org/pan2) Cancel-Lock: sha1:fc3vMaUbqfvlzDVS3ciLeKpMrLY= Xref: csiph.com comp.lang.python:81731 On Sat, 29 Nov 2014 11:35:18 -0800, Gautam R Bharadwaj wrote: > Here is the code in python, this code arranges the alphabets in > descending order and now I want to encode each alphabet with 0 and next > alphabet with 1, then 00,01,10,11,000,001 and so on. Please help me with > that. > > ////////////////////////////////////////////////////////// > //////////////////CODE//////////////////////// > > from collections import defaultdict import string text ='intalks is an > organization comprised of passionate > students'.lower().translate(None,string.punctuation+' ') > c = defaultdict(int) > c.update({letter:0 for letter in string.lowercase[:26]}) > for letter in text: > c[letter] += 1 > > for letter,freq in sorted(c.iteritems(),key=lambda (l,f): (-f,l)): > print freq, letter If you can confirm that the following: 0001111000011100001011001111010100001111100100110010000101001100000000110101010100001001111000011001000100001110 is the output you want in terms of the original string encoded according to your encoding scheme, then your problem domain currently is two fold: a) To write a function that generates the encoding b) To apply the encoding to the original string To achieve the former, I used a combination of determining a string length, converting a string to an integer using base 2, converting a number to a string in binary form, and generating arbitrary length strings of zeroes. Not necessarily all done in the most pythonic of manners I must admit. Have you attempted anything to generate you string of encodings yet? Because until you show a good faith effort, you don't my solution to your homework problem for free. To achieve the latter, I took the encodings and strung them together according to the letters they represented in the original string. -- Denis McMahon, denismfmcmahon@gmail.com