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


Groups > comp.lang.python > #99120

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 shorten "compress" long integer to short ascii.
Date Thu, 19 Nov 2015 20:45:08 -0700
Lines 49
Message-ID <mailman.503.1447991136.16136.python-list@python.org> (permalink)
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de KVL9mabkSqX6PqrKHhDmsAbudK207m9tnlV7mC/xNeSg==
Return-Path <vincent@vincentdavis.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'resulting': 0.04; 'utf8': 0.09; 'received:209.85.218': 0.10; 'def': 0.13; '"\\\\"': 0.16; 'ls:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'shorten': 0.16; 'string': 0.17; '&lt;': 0.18; 'example.': 0.18; 'integer': 0.18; 'to:name:python- list@python.org': 0.20; 'ascii': 0.22; "skip:' 40": 0.22; 'help.': 0.23; 'import': 0.24; 'message-id:@mail.gmail.com': 0.27; 'code:': 0.29; 'random': 0.29; 'code': 0.30; 'int': 0.33; 'shorter': 0.33; 'this?': 0.34; 'gets': 0.35; 'received:google.com': 0.35; 'could': 0.35; 'something': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'subject:" ': 0.36; 'to:addr:python-list': 0.36; 'skip:& 10': 0.37; 'suggestion': 0.37; 'received:209': 0.38; "didn't": 0.39; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'side': 0.62; 'goal': 0.64; 'subject:long': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=vincentdavis.net; s=google; h=mime-version:from:date:message-id:subject:to:content-type; bh=AL0OE99p/kF7QXJbrDHEBHRA6S0NPGtMDMXoB1wMKow=; b=DZN5TAWPDawLczpQqyWbEvAcMkTg1pD0NlLsSuy1i9Mokjxpir8/zLj+wAPU1Vp+c2 cr07gtEFtImd3rGeMZBJnG98VZ4pGmc149wxyBVr506XVlkBcoJ2808ONHmKDAVPSpwo AuRqTA3wje4WKS+IQfG3Mm1zsvEWmAwFxoqdA=
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to :content-type; bh=AL0OE99p/kF7QXJbrDHEBHRA6S0NPGtMDMXoB1wMKow=; b=YzoKTe0AAmRV0bGiKq8frasFVHbG5XBUC8rES5rFyiuKzX9fpi6VoJ1bihamFUa4cA 73HDxkdUJ8l4v+AJtwrW3dE+cVWpOJ0YSzxp33zhagsskm4G07QiAUoQHOIUBZ6/iZqU r1X82aW554aQhB615+VvoWEbXMr2Bji9ckaOq4ioa3srOqODdqc7b9Uav5hd7KHZeT4f jxsN95gFlK8IUGN+rd+ZXCdu51Y5jV0GUP6HpjBLjgs/8oB5gymR1zoS+4gsQvq4YXwv XcVM2RFbm3pu+Csj8Nkgdcrk1bTOaZr4rfAaqq56HKZrsVMYkP0J45zz62yh6Q3Fk9G8 F/+A==
X-Gm-Message-State ALoCoQlSfSJidUMjM48ud9QC09ufl+dDw8kL23J0Yl32No4Vu1IinJkNFWvRGlHzh3YPAdpy7AQm
X-Received by 10.202.77.80 with SMTP id a77mr7130594oib.72.1447991127988; Thu, 19 Nov 2015 19:45:27 -0800 (PST)
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:99120

Show key headers only | View raw


My goal is to shorten a long integer into a shorter set of characters.
Below is what I have which gets me about a 45-50% reduction. Any suggestion
on how to improve upon this?
I not limited to ascii but I didn't see how going to utf8 would help.
The resulting string needs to be something I could type/paste into twitter
for example.

On a side note string.punctuation contains "\\" what is \\ ?

import string
import random

# Random int to shorten
r = random.getrandbits(300)
lenofr = len(str(r))

l = string.ascii_lowercase + string.ascii_uppercase +
'!"#$%&\'()*+,-./:;<=>?@[]^_`{|}~'
n = [str(x) for x in list(range(10,93))]
decoderdict = dict(zip(l, n))
encoderdict = dict(zip(n, l))

def encoder(integer):
    s = str(integer)
    ls = len(s)
    p = 0
    code = ""
    while p < ls:
        if s[p:p+2] in encoderdict.keys():
            code = code + encoderdict[s[p:p+2]]
            p += 2
        else:
            code = code + s[p]
            p += 1
    return code

def decoder(code):
    integer = ""
    for c in code:
        if c.isdigit():
            integer = integer + c
        else:
            integer = integer + decoderdict[c]
    return int(integer)

short = encoder(r)
backagain = decoder(short)

print(lenofr, len(short), len(short)/lenofr, r==backagain)

Back to comp.lang.python | Previous | NextNext 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