Help needed

Denis McMahon denismfmcmahon at gmail.com
Sun Nov 30 07:28:55 EST 2014


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 at gmail.com



More information about the Python-list mailing list