I want to convert a sequence of numbers to a single number which will retain individual values as well as their position. e.g. the following sequence is provided-
1,6,7,8,9,45,67
here,take for example,if I apply simple addition i.e. 1+6+7+8+9+45+67 then a number will be generated. But from that no. we can’t extract the individual numbers with their ordering[i.e. 1,6,7,8,9,…].
Is there any way to achieve this feature without any ambiguous deduction [i.e only 1 unique set of numbers will be extracted from a number.]? Is there any mathematical function which will be helpful to get back the individual elements from that number?
You can convert this to a base-N number, where N is one larger than the largest value that will appear in your input sequence.
UPDATE
Based on the various comments, I would like to offer an alternative solution that may be easier to implement. You can consider the sequence to be a UTF-8 encoded string and use Huffman coding with a custom dictionary to achieve a compact representation.
The custom dictionary allows you to store very common characters with very few bits (for example, the sequence separator ‘,’ and the individual characters ‘0’..’9′ can be stored with as few as 3 bits, but also other numbers that you find to be statistically likely to occur can be stored in a short bit sequence. For example, if you find that “42” occurs frequently, you could store “42” in just a few bits.
If you only assign special codes to ‘,’ and ‘0’ to ‘9’, you will average less than 4 bits per character in the input string while retaining the comma separating sequence members. Finding common, multi-character substrings and adding them to the dictionary will only improve on that ratio.
Using a custom dictionary also means that you do not have to store the dictionary in the header of the compressed data, since it is well-known to you.
I did something just like this using SharpZipLib
http://www.icsharpcode.net/opensource/sharpziplib/
http://community.sharpdevelop.net/forums/p/8255/23219.aspx
It is also easy to do with zlib
Compressing small piece of data