I have 9 products. A basket can contain any combination of products such that each product appears 0-9 times in the basket. For example, basket1 might contain 0 of product1, 3 of product2, 1 of product3, etc.
I want to generate a unique BasketId for each basket. Rather than doing the obvious Id which, for the above example would be a long string like "product1_0 product2_3 ..." I want to use a numeric Id for storage efficiency.
Since there are 9 products and can each occur 10 times, there should be 10^9 possible combinations. So I should be able to create a mapping like
get_basketId <- function(p1, p2, p3, p4, p5, p6, p7, p8, p9){
# given the count of each product, return a unique numeric id
}
which is also invertible
get_product_counts <- function(get_basketId){
# given a basketId, return the count of each product
}
Any suggestions how to do this?
Note also that Cantor Pairing is one theoretical solution, but unfortunately Cantor Pairing results in unnecessarily large IDs that overflow.