Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99120
| From | Vincent Davis <vincent@vincentdavis.net> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | shorten "compress" long integer to short ascii. |
| Date | 2015-11-19 20:45 -0700 |
| Message-ID | <mailman.503.1447991136.16136.python-list@python.org> (permalink) |
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 | Next — Next in thread | Find similar | Unroll 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