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


Groups > comp.lang.python > #99151

Re: shorten "compress" long integer to short ascii.

From Vincent Davis <vincent@vincentdavis.net>
Newsgroups comp.lang.python
Subject Re: shorten "compress" long integer to short ascii.
Date 2015-11-20 07:18 -0700
Message-ID <mailman.517.1448029163.16136.python-list@python.org> (permalink)
References <mailman.503.1447991136.16136.python-list@python.org> <871tbll2hw.fsf@nightsong.com>

Show all headers | View raw


On Thu, Nov 19, 2015 at 9:27 PM, Paul Rubin <no.email@nospam.invalid> wrote:

> You can't improve much.  A decimal digit carries log(10,2)=3.32 bits
> of information.  A reasonable character set for Twitter-style links
> might have 80 or so characters (upper/lower alphabetic, digits, and
> a dozen or so punctuation characters), or log(80,2)=
>

​Where do I find out more about the how to calculate information per digit?
​
Lots of nice little tricks you used below. Thanks for sharing.


> Here is my shortened version:
>
>   import string
>
>   # alphabet here is 83 chars
>   alphabet = string.ascii_lowercase + \
>        string.ascii_uppercase +'!"#$%&\'()*+,-./:;<=>?@[]^_`{|}~'
>   alphabet_size = len(alphabet)
>
>   decoderdict = dict((b,a) for a,b in enumerate(alphabet))
>
>   def encoder(integer):
>       a,b = divmod(integer, alphabet_size)
>       if a == 0: return alphabet[b]
>       return encoder(a) + alphabet[b]
>
>   def decoder(code):
>     return reduce(lambda n,d: n*alphabet_size + decoderdict[d], code, 0)
>
>   def test():
>       n = 92928729379271
>       short = encoder(n)
>       backagain = decoder(short)
>       nlen = len(str(n))
>       print (nlen, len(short), float(len(short))/nlen)
>       assert n==backagain, (n,short,b)
>
>   test()
>




Vincent Davis
720-301-3003

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

shorten "compress" long integer to short ascii. Vincent Davis <vincent@vincentdavis.net> - 2015-11-19 20:45 -0700
  Re: shorten "compress" long integer to short ascii. Paul Rubin <no.email@nospam.invalid> - 2015-11-19 20:27 -0800
    Re: shorten "compress" long integer to short ascii. Vincent Davis <vincent@vincentdavis.net> - 2015-11-20 07:18 -0700
      Re: shorten "compress" long integer to short ascii. Grant Edwards <invalid@invalid.invalid> - 2015-11-20 15:55 +0000

csiph-web