Wednesday, August 13, 2008

A Brainteaser

Say there are a bunch of different types of things. Apple, Banana, Car, Door, etc. The different possible combinations of those things can easily be mapped to whole numbers by using binary. For example, maybe 1 means Apple and 1010 (binary) means Door and Banana. If you give me the list of different types of things (with the order of the list implying which binary digit represents each one) and a number, I can tell you exactly which set of things that number represents. "3", given the above list, maps to Banana and Apple, because 3 written in binary is 11.

OK, so that works.

Now what if you introduce the idea that there could be any number of all those things, not just 1 or 0? Like, maybe I have 12 Apples and 44,392,105 Doors. How do you map the non-negative integers to that space, 1 to 1? This is a bit trickier because Base Two works better than Base Arbitrarily-Many.

See if you can figure out a way. It's quite doable.

3 comments:

Julenka said...

Would you know how many objects you're mapping? If you did, then you could map as follows:
If you had 5 Apples, 20 bears, and 3 chairs, you woulc get
593 090 020. Each set of 3 represents the part of the total count. You essentially break the number into sections based on how many items you have. The first item in each section is represented by the first digit, the second by the second digit, and so on. Then, just sum across these digits to get the total counts. Granted this is a very large number, but it would work...

Sean McCarthy said...

Nope, you need to support any number of items. The only thing you know ahead of time is the ordered list of different types of things.

There's a way!

Sean McCarthy said...

I didn't read your answer and my original question well enough before replying - let me try again.

I would actually say your solution does work - a person given your generated number, the list of object types, and knowledge of your system could determine it. So good job!

There is another solution which I am thinking of that will also work when the list of object types is infinite. That wasn't a requirement in the question but it's cool.