Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Vincent Davis 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: References: <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: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99151 On Thu, Nov 19, 2015 at 9:27 PM, Paul Rubin wrote= : > You can't improve much. A decimal digit carries log(10,2)=3D3.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)=3D > =E2=80=8BWhere do I find out more about the how to calculate information pe= r digit? =E2=80=8B Lots of nice little tricks you used below. Thanks for sharing. > Here is my shortened version: > > import string > > # alphabet here is 83 chars > alphabet =3D string.ascii_lowercase + \ > string.ascii_uppercase +'!"#$%&\'()*+,-./:;<=3D>?@[]^_`{|}~' > alphabet_size =3D len(alphabet) > > decoderdict =3D dict((b,a) for a,b in enumerate(alphabet)) > > def encoder(integer): > a,b =3D divmod(integer, alphabet_size) > if a =3D=3D 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 =3D 92928729379271 > short =3D encoder(n) > backagain =3D decoder(short) > nlen =3D len(str(n)) > print (nlen, len(short), float(len(short))/nlen) > assert n=3D=3Dbackagain, (n,short,b) > > test() > Vincent Davis 720-301-3003