Reputation: 1127
This function takes in any base-10 integer and returns the string representation of that number in its specified base-32 form:
def encodeN(n,N,D="0123456789qwertyuiopasdfghjklzxc"):
return (encodeN(n//N,N)+D[n%N]).lstrip("0") if n>0 else "0"
Example:
print (encodeN(16002,32))
Output:
ya2
But I have a problem with writing a decoding function from base-32 back to base-10. How can I write it? Can I enter custom nonstandard characters to extend the base-n?
Upvotes: 4
Views: 7021
Reputation: 54223
With the same recursive structure, you could write:
def encodeN(n,N,D="0123456789qwertyuiopasdfghjklzxc"):
return (encodeN(n//N,N)+D[n%N]).lstrip("0") if n>0 else "0"
def decodeN(n,N,D="0123456789qwertyuiopasdfghjklzxc"):
return decodeN(n[:-1],N) * N + D.index(n[-1]) if n else 0
It seems to work fine:
print(encodeN(16002, 32))
# "ya2"
print(decodeN("ya2", 32))
# 16002
print(all(decodeN(encodeN(x, b), b) == x for b in range(2, 33) for x in range(10000)))
# True
print(all(encodeN(decodeN(str(x),32), 32) == str(x) for b in range(2, 33) for x in range(10000)))
# True
It's not very efficient though. Using a dict like MartijnPieters would be a better idea than using str.index
.
Upvotes: 2
Reputation: 584
>>> import string
>>> len(string.readable)
100
Judging by this you could have up to base 100 with no issues like duplicating characters or changing the encoding. But if we take out \t\n\r\x0b\x0c
we get to 94.
Besides this you would have to result to some kind of custom rules, duplicating characters or prefixing them and such.
Upvotes: 1
Reputation: 1121924
You could cheat:
tmap = str.maketrans('qwertyuiopasdfghjklzxc', 'abcdefghijklmnopqrstuv')
result = int(inputvalue.translate(tmap), 32)
Demo:
>>> tmap = str.maketrans('qwertyuiopasdfghjklzxc', 'abcdefghijklmnopqrstuv')
>>> inputvalue = 'ya2'
>>> int(inputvalue.translate(tmap), 32)
16002
int()
is perfectly capable of translating arbitrary bases back to integer values; all you need to do is use the standard progression of letters. The str.translate()
call above maps your custom progression to the standard.
Otherwise, take each character from your input string, starting at the left, map that to an integer from your character map, and multiply by the base N each time:
def decodeN(n, N, D={c: i for i, c in enumerate("0123456789qwertyuiopasdfghjklzxc")}):
result = 0
for c in n:
result = (result * N) + D[c]
return result
This is the slower option; str.translate()
and int()
both use optimised C code to do their jobs, and will always be faster than a pure-python approach.
Translating that to a recursive version to match your encodeN()
implementation:
def decodeN(n, N, D={c: i for i, c in enumerate("0123456789qwertyuiopasdfghjklzxc")}):
return decodeN(n[:-1], N) * N + D[n[-1]] if n else 0
Upvotes: 4