equation to create unique value

108 Views Asked by At

I have a list of n objects

say [ apple, orange, carrot, cherry, banana ]

Now I am trying to come up with an equation which will generate an unique number for each combinations from this list.

eg :

say I assign following values for each items

apple = 1
orange = 2
carrot = 3
cherry = 4
banana = 5

If I directly add the individual values to generate combination score then the values will be

apple      = 1
apple, carrot = 4
apple, orange = 3

But here multiple combination can have same scores.

I need to generate an equation which will generate UNIQUE score for all of these combination.

POSITION IS NOT AN ISSUE.

Score(apple,orange,carrot) = Score(carrot, apple, orange) = Score (orange, apple, carrot)

But Score (apple, carrot) $\ne$ Score(cherry)

How this can be generated ?

2

There are 2 best solutions below

4
On BEST ANSWER

Assign a prime number to each object.

Let the score for each combination be the product of the prime numbers assigned to the objects in the combination.

This will also allow you to use the score to record multiple occurrences of items (if you want).

Every such score created can be uniquely factorised to identify the items in your list.

1
On

Perhaps I'm missing something, but it sounds as if all you want is to assign each element a unique exponentiation of a base value; for example,

  • apple = 1 i.e. $2^0$
  • banana = 2 i.e. $2^1$
  • cherry = 4 i.e. $2^2$

etc. Then any way you add them, you can always decompose the sum. More simply:

  • apple = 1
  • banana = 10
  • cherry = 100