Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #81720 > unrolled thread

Help needed

Started byGautam R Bharadwaj <gautamrbharadwaj@gmail.com>
First post2014-11-29 11:35 -0800
Last post2014-11-30 12:28 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Help needed Gautam R Bharadwaj <gautamrbharadwaj@gmail.com> - 2014-11-29 11:35 -0800
    Re: Help needed Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-30 12:28 +0000

#81720 — Help needed

FromGautam R Bharadwaj <gautamrbharadwaj@gmail.com>
Date2014-11-29 11:35 -0800
SubjectHelp needed
Message-ID<7d92e814-9d27-4f7d-a189-49fa84e51a34@googlegroups.com>
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
        

[toc] | [next] | [standalone]


#81731

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-11-30 12:28 +0000
Message-ID<m5f2i7$dj1$4@dont-email.me>
In reply to#81720
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web