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.

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Vincent Davis <vincent@vincentdavis.net>
Newsgroups comp.lang.python
Subject Re: shorten "compress" long integer to short ascii.
Date Fri, 20 Nov 2015 07:18:55 -0700
Lines 50
Message-ID <mailman.517.1448029163.16136.python-list@python.org> (permalink)
References <mailman.503.1447991136.16136.python-list@python.org> <871tbll2hw.fsf@nightsong.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-Trace news.uni-berlin.de kqnGKn4m6/HoKmeneL7O3wdQhpPEvgemfQqwCwtpCx6g==
Return-Path <vincent@vincentdavis.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.039
X-Spam-Evidence '*H*': 0.92; '*S*': 0.00; 'bits': 0.07; 'received:209.85.218': 0.10; 'def': 0.13; 'thu,': 0.15; 'alphabet': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'test()': 0.16; 'test():': 0.16; 'wrote:': 0.16; 'string': 0.17; 'skip:+ 50': 0.18; '2015': 0.20; 'to:name:python-list@python.org': 0.20; 'code,': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'paul': 0.24; 'message-id:@mail.gmail.com': 0.27; 'decimal': 0.29; 'character': 0.29; 'print': 0.30; "can't": 0.32; 'version:': 0.33; 'received:google.com': 0.35; 'nov': 0.35; 'received:209.85': 0.36; 'subject:" ': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'skip:9 10': 0.37; 'received:209': 0.38; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'per': 0.62; 'skip:n 10': 0.62; 'more': 0.63; 'information': 0.63; 'here': 0.66; 'below.': 0.66; 'subject:long': 0.84; 'carries': 0.91; 'dozen': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=vincentdavis.net; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=EsAn2gm8burCWHcLpMuGwvPZYiWxGFBYtkwRO1gM9UI=; b=kZ8rgYurhEG4lWi5P8V76dJSl7Qivl62kKG3snJZ4qeVIh9RrBo6Zm7axDvjZFp3EG kbCQT4l2dcD5wNI9IsAF5Ojg5soPC7OhhXDrUtHN5Wkkwj3E83IOUQkhN/+AP6s0VQEZ k6UcUVTlY2A3zapsvVZw2wFivPckswV7JLKs4=
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type; bh=EsAn2gm8burCWHcLpMuGwvPZYiWxGFBYtkwRO1gM9UI=; b=cQLBRFJIeVgumFqk4FQ8YwJ3A+DO3zpiw79BjkFb7/UufHkmosoq7rY87qfxUt83fu lTFxX0oCMzRGK9KAUk+TaSokmkz2pV4fjQKWoVAs4ZF7DOqwVvuo6qGj8gv24MFfV4Ot bCcC4aETgpgUvp6mOKkNWjQgzk1aKXi0OU5XPITJQ9CgGzCDqZ9ovmq33qXgS42kBmYZ KO7GKciNGA6m47SbWI0kUqcWwVSLDdYMUmwYUPOQGmJEIhVJeSMkY9ngT1nJTHbucMqu 6T4nkKLuxHkpy2MkbN/lp5wI+eMVfYIuVMpseUR2958BqczYRsKGxarMGojgLW121fn7 Zvyg==
X-Gm-Message-State ALoCoQl++48AmeXdQwH3WmjZXnqMkzTSiA3ym7dLRiOAiebWt5pFZVo+jdD0OP3t4khvxHbOkTwm
X-Received by 10.202.72.17 with SMTP id v17mr8879648oia.68.1448029155200; Fri, 20 Nov 2015 06:19:15 -0800 (PST)
In-Reply-To <871tbll2hw.fsf@nightsong.com>
X-Content-Filtered-By Mailman/MimeDel 2.1.20+
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:99151

Show key headers only | 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