# Our Blog

Ongoing observations by End Point people

## A Product Variant Code Challenge

By Steph Skardal
April 27, 2011

A while ago, I came across a cute little Ruby challenge that looked interesting:

*

Given an array of arrays of possible values, enumerate all combinations that can occur, preserving order. For instance:

Given: [[1,2,3], [4,5,6], [7,8,9]], calculate the same result as the code below, but do so with an arbitrary size array:

``````combos = []
[1,2,3].each do |v1|
[4,5,6].each do |v2|
[7,8,9].each do |v3|
combos << [v1, v2, v3]
end
end
end
combos
``````

Entries can be written using one or more functions, and may optionally be written as a class extension (i.e. Array)....

*

And now some context to why I thought this was applicable to ecommerce: Letâ€™s imagine you have a product. Then, letâ€™s imagine that the product has variations, or variants. Weâ€™ll say we have an arbitrary size array of â€śoption typesâ€ť, each with an arbitrary number of items in the array. Here, we have option types of size, color, and printed logo, which yields multiple variations, variants or combinations of a single product:

Size Color Logo
Large
Â
Red
Medium
Â
Blue
Small

And letâ€™s give a real-life example data model, this one from a previous article on Spreeâ€™s product data model:

Given this context, we can say that the outer array dimension represents product option types with individual values and that this challenge is asking for a simple method to create a list of all possible variants or variations of the product. In our t-shirt example, the solution to the example is:

``````[["large", "blue", "twitter"],
["large", "blue", "tumblr"],
["large", "blue", "blogger"],
["large", "red", "tumblr"],
["large", "red", "blogger"],
["medium", "blue", "tumblr"],
["medium", "blue", "blogger"],
["medium", "red", "tumblr"],
["medium", "red", "blogger"],
["small", "blue", "tumblr"],
["small", "blue", "blogger"],
["small", "red", "tumblr"],
["small", "red", "blogger"]]
``````

Unfortunately, the original contest only received 2 submissions, so I wanted to open the door here to allow people to submit more submissions in Ruby and any other language. Please include a link to the gist or code solution and in a few weeks, Iâ€™ll update the post with several submissions and links to the submissions, including my own solution. Itâ€™s a nice little puzzle to take on in your desired language.