1

I am trying to encode a int list to a unique number using following logic

b=28
l=[6, 4, 23, 28, 26, 9, 7, 16, 2]
sum=0
for i,n in enumerate(l):
   sum+=(28**i)*n

This generates a huge number. how can i get back the original list from this number? I have tried the following code

result=[]
while sum!=0:
    n=sum%28
    result.append(n)
    sum=sum//28
print(result)

However the result prints [6, 4, 23, 0, 27, 9, 7, 16, 2]

2 Answers 2

2

Your encoding method is basically a base-28 conversion and can only allow integers no greater than 27; otherwise it would carry over to the next number, as evident by your decoded sequence, where 28 becomes 0 and the next number, 26, becomes 27. There's no fix for your code. You just have to come up with a new encoding algorithm if you want to be able to accommodate numbers greater than 27.

Sign up to request clarification or add additional context in comments.

Comments

0

The way you encode the list is similar to converting a base-28 number to decimal number. Since the largest number in the list is 28 itself, it is proper to encode the list with base-29 i.e (Largest_number+1). So you can use the following code to encode.


l=[6, 4, 23, 28, 27, 9, 7, 16, 2]
b=max(l)+1
sum=0
for i,n in enumerate(l):
    sum+=(b**i)*n

and for decoding,

result=[]
while sum!=0:
    n=sum%b
    result.append(n)
    sum=sum//b
print(result)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.