0

I need function that maps any m integers between a and b (where b-a > m) into integers between 0 to m-1. The m integers between a and b may not be in any order. The mapping could be in any order as long as it is one-to-one mapping.

For example I have a set of integers between 10 and 50 and I pick any 10 integers randomly and map them into 0-9. The function could take one, two or three inputs that may different for each set of those 10 integers. And one more thing, it has to be reversible, i.e using those inputs I can get back the original number.

does it exist of such function and is it possible ?

2
  • are you looking for pure computational function or some control flow logic can be involved? Can you use some data structure to maintain mapping? Commented Jun 17, 2011 at 2:08
  • What does it mean The function could take one, two or three inputs? One of those inputs could be the list of random numbers ... and you are done (see PengOne's answer) Commented Jun 17, 2011 at 2:45

2 Answers 2

1

It's fairly easy. Map the smallest number to 0, the second smallest to 1, etc. The map is invertible if and only if you know the set of numbers you began with.

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

5 Comments

So ... what is the importance of the set being bounded ..?
@belisarius: i don't understand your question.
I am trying to understand the question. See my comment to the question itself. Half the facts the OP provided are not needed for your algorithm. (I am not saying it's wrong, just thinking why the OP stated that the set is bounded between known numbers, etc)
@belisarius: I just assumed that he was taking a random sample of N integers in some range and wanting to map them down to 0...N-1. No idea if that's what he means or not, but it was the only way I could make sense of the question.
Perhaps you're right. Also, the requirement for The function could take one, two or three inputs seems quite strange :)
1

It sounds like you're asking for a minimal perfect hash. Such functions do exist, there are algorithms for finding them, and even preexisting libraries to do the work.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.