Introduction
In computer science, a literal is a notation for representing a fixed value in source code. Almost all programming languages have notations for atomic values, some also have notations for elements of enumerated types and compound values. Wikipedia
For example 1
usually represent an integer value, "Hello"
a string, [9,5,11]
an array and 1..9
a range.
The range notation is special because we have just two values in the literal but the actual value includes all elements in between.
We can say that a range expands to an array or a list of values. So the expansion of the range 1..9
is [1,2,3,4,5,6,7,8,9]
.
In this challenge you are given a Cartesian literal as input and you have to output its expansion.
Notation format rules
consider only non negative integers values.
this notation could work for products of any degree but in this challenge you have to handle only products of two sets, so we get a list of pairs.
we can have one or more groups of products. Every group is terminated by the
/
symbol and generates its own list which is then concatenated to the others groups.each group has 2 sets: A and B and they are separated by the
:
symbol.each set is composed of ranges and/or atomic values separated by
,
.
Ranges are in the formstart-end
for example0-10
.Values must be sorted without overlaps, for example
1-5,5,4
can not appear.every group contains non empty sets.
Example
The literal 1-2,5:10-12/0:1-3/
is composed of two groups.
The first group (1-2,5:10-12
) has the sets:
A=[1,2,5]
B=[10,11,12]
and generates the product
[1,10],[1,11],[1,12],[2,10],[2,11],[2,12],[5,10],[5,11],[5,12]
the second group generates [0,1],[0,2],[0,3]
which is appended to the first so the output is:
[[1,10],[1,11],[1,12],[2,10],[2,11],[2,12],[5,10],[5,11],[5,12],[0,1],[0,2],[0,3]]
Test cases
"0:0/" -> [[0,0]]
"1-3:2/" -> [[1,2],[2,2],[3,2]]
"4:5-6/" -> [[4,5],[4,6]]
"9,10,11:9-11/" -> [[9,9],[9,10],[9,11],[10,9],[10,10],[10,11],[11,9],[11,10],[11,11]]
"100:0-1,2,3-4/1:2/" -> [[100,0],[100,1],[100,2],[100,3],[100,4],[1,2]]
"1:2/3:4/5:6/7:8/9:10/" -> [[1,2],[3,4],[5,6],[7,8],[9,10]]
"11-13:2/" -> [[11,2],[12,2],[13,2]]
Rules
- This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
- You can assume the input will always be a valid literal, you don't have to handle invalid literals.