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 Java
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 Java will work.
Create a new class file;
Call it ConditionalSelection
.
Type out the adjoining Java code for Selection with Conditioned Repetition.
Java code for ConditionalSelection Class
package miscellaneous;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class ConditionalSelection extends Selection {
private List<String[]> final_elements;
public ConditionalSelection() {
super();
}
public List<String[]> limitedSelection(String[] candidates, int size, int[] minimum, int[] maximum) {
final_elements = new ArrayList<>();
groupSelection(candidates, size);
List<String> members;
for (int i = 0; i < complete_group.size(); i++) {
members = new ArrayList<>();
members.addAll(Arrays.asList(complete_group.get(i)));
boolean state = false;
for (int j = 0; j < words.length; j++) {
int count = Collections.frequency(members, words[j]);
if (count >= minimum[j] && count <= maximum[j]) {
state = true;
} else {
state = false;
break;
}
}
if (state) {
final_elements.add(complete_group.get(i));
}
}
return final_elements;
}
}
Main Class
package miscellaneous;
import java.util.Arrays;
import java.util.List;
public class Miscellaneous {
public static void main(String[] args) {
String[] goods = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
ConditionalSelection choose = new ConditionalSelection();
List<String[]> result = choose.limitedSelection(goods, 4,
new int[]{0, 0, 1, 0, 0, 1, 0, 0, 1, 0}, new int[]{4, 3, 2, 4, 3, 2, 4, 3, 2, 4});
System.out.println(Arrays.toString(choose.words) + " conditioned selection " + choose.r + ":\n");
int i = 0;
for (String[] set : result) {
System.out.println(++i + ": " + Arrays.toString(set) + ";");
}
System.out.println("\nNumber of ways is " + result.size() + ".");
}
}