Context :
Suppose you have a sheet of paper measuring 8 x 10
. You want to cut it exactly in half while maintaining its rectangular shape. You can do this in two ways.
- You can cut it in half preserving its long dimension of 10 (for our purpose we will refer to this as long cut from now on).
Example : $$ [8, 10] \rightarrow {long cut} \rightarrow [4, 10] $$ Or you can cut it in half preserving its short dimension (we will refer to it as short cut).
Example :
$$ [8,10]→short cut→[5,8] $$
For a square, the short and long cut are same. i.e : $$ [12,12]→long cut→[6,12] $$ $$ [12,12]→short cut→[6,12] $$
Task :
For this challenge you are given two arguments.
- The first is a string containing the cuts to be made to a sheet of paper in sequence from first to last. A long cut is designated by "L" and a short cut by "S".
- The second is dimension of paper after said cuts were made (an array)
Given that devise a function that will find the all the possible original dimensions of the sheet of paper before any cuts were made.
The dimensions of a sheet are given as an array [x,y]
(\$y\geq x\$)
Return all possible orignial paper measures : [x, y]
for (\$y\geq x\$)
If input make it so that an output is not possible return a falsey value (be it empty array , false, 0, empty string, nil/null/Null). You may not give an error though
Examples :
cuttingPaper("S", [3, 7]) --> [[3, 14]]
cuttingPaper("L", [5, 7]) --> []
cuttingPaper("", [3, 7]) --> [[3, 7]]
cuttingPaper("S", [5, 7]) --> [[5, 14], [7, 10]]
cuttingPaper("LSSSSS", [1, 2]) --> [[2, 64], [4, 32], [8, 16]]
Explanation :
For example 2 :
L
for [5, 7]
gives empty array since if it started [5, 14]
then the long cut would result in [2.5, 14]
and if it started with [10, 7]
then the long cut would result in [10, 3.5]
. so in this case the cut is simply not possible
For example 4 :
[5, 7]
for S
cut gives 2 solutions since it could start as [5, 14]
and then short cut would yield [5, 7]
which is possible solution 1.
Alternately you can start with [7, 10]
and then cut and you would end up with [5, 7]
which is possible solution 2. B
ecause of that you get 2 possible solutions. (you output both)
In such a case you will return an array of array / list of list / etc...
I/O :
You take 2 inputs :
- A String containing the cuts made.
- The array after said cuts were made
You can take it as the following :
- an array/set (or equivalents) containing 3 inputs (in following order
[cuts, x, y]
or[x, y, cut]
- Three inputs
cut, x, y
orx, y, cut
- 2 input
cut, [x, y]
or[x, y], cut
- A string consisting of all 3
cuts x y
(space seperated, you may choose your separator)
Notes :
- The string will either be empty or contain some combination of S and/or L.
- The array will always contain two Positive integers.
- This is code-golf so shortest answer in bytes will win (note : I won't be selecting any answer as accepted).
- Standard loopholes apply.
For those that solve it kindly if you can give a small explanation so others (mainly me) can learn/understand your code (if you have the time and patience)