Conditional Selection with Limits on Repetition.
So it's Christmas, and your mum delegates you, for some mysterious
reason, to go buy nine (9) balloons - of any of the colours
red, orange, yellow, green, blue, indigo,
violet, pink, milk, white,- for home decoration.
But here's the catch. You are to buy:
-
a minimum of 1 red balloon
and a maximum of 4 red balloons;
-
a minimum of 1 orange balloon
and a maximum of 3 orange balloons;
-
a minimum of 1 yellow balloon
and a maximum of 2 yellow balloons;
-
a minimum of 1 green balloon
and a maximum of 4 green balloons;
-
a minimum of 1 blue balloon
and a maximum of 3 blue balloons;
-
a minimum of 1 indigo balloon
and a maximum of 2 indigo balloons;
-
a minimum of 1 violet balloon
and a maximum of 4 violet balloons;
-
a minimum of 1 pink balloon
and a maximum of 3 pink balloons;
-
a minimum of 1 gray balloon
and a maximum of 2 gray balloons;
-
a minimum of 1 white balloon
and a maximum of 4 white balloons.
With these conditions, every family member's favourite colour
is covered for, and the decoration mix is also kept lively.
This is quite a handful for you to handle...
Code for Limited Repetitive Selection in JavaScript
The code for Selection with Conditioned Repetition will be
based on that for Selection with boundless Repetition.
All that is needed after Selection with limitless Repetition
is a Productive, as opposed to Summative, check
of the results from the Selection with unconditioned Repetition
for those options that meet our conditions.
This is how our Limited Repetitive Selection algorithm in JavaScript will work.
Create 2 new files; On Notepad++: File, New.
Call them ConditionalSelection.html
and COnditionalSelection.js
respectively.
Type out the adjoining JavaScript code for Selection with Conditioned Repetition.
ConditionalSelection - .html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Limited Selection</title>
<script src="Selection.js"></script>
<script src="ConditionalSelection.js"></script>
</head>
<body>
<h3>Selections with Limits to Repetition</h3>
<div id="limit_select"></div>
<script>
var result = limitedSelection([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 4,
[0, 0, 1, 0, 0, 1, 0, 0, 1, 0], [4, 3, 2, 4, 3, 2, 4, 3, 2, 4]);
var print = "", set, count = 0;
for (set in result) {
print += ++count + ": [" + result[set].join(", ") + "]<br/>";
}
document.getElementById("limit_select").innerHTML +=
words.join(", ") + " conditional selection " + r + ":<br/><br/>" + print +
"<br/><br/>Number of ways is " + count + ".";
</script>
</body>
</html>
JavaScript code for ConditionalSelection - .js
function limitedSelection(candidates, size, minimum, maximum) {
var final_elements = {};
selection = groupSelection(candidates, size);
var x, k = 0;
for (x in selection) {
var state = false;
var members = selection[x].slice(0);
for (var y = 0; y < words.length; y++) {
var count = 0, i = 0, j = 0;
while ((i = members.slice(j).indexOf(words[y])) > -1) {
count++;
j += i + 1;
}
if (count >= minimum[y] && count <= maximum[y]) {
state = true;
} else {
state = false;
break;
}
}
if (state) {
final_elements[k++] = selection[x];
}
}
return final_elements;
}