I am trying to make a url from a different combinations of string separated by comma so that I can use those url to execute them and get the data back.
I have simplified something like this, I have a HashSet that will contain all my strings, not A,B,C in real. I just modified it here to make it simple.
Set<String> data = new HashSet<String>();
h.add("A");
h.add("B");
h.add("C");
for (int i = 1; i < 1000; i++) {
String pattern = generateString(data);
String url = "http://localhost:8080/service/USERID=101556000/"+pattern;
System.out.println(url);
}
/**
* Below is the method to generate Strings.
/
private String generateString(HashSet<String> data) {
//not sure what logic I am supposed to use here?
}
So the output should be something like this-
http://localhost:8080/service/USERID=101556000/A
http://localhost:8080/service/USERID=101556000/B
http://localhost:8080/service/USERID=101556000/C
http://localhost:8080/service/USERID=101556000/A,B,C
http://localhost:8080/service/USERID=101556000/B,C
http://localhost:8080/service/USERID=101556000/C,A
--
And other combinations
The above output can be in any random order as well. But it should be all the possible combinations. And if all the possible combinations are finished then start again.
Any suggestion how can I achieve the above problem definition?
Given what is k-arrangement (http://en.wikibooks.org/wiki/Probability/Combinatorics), you are looking for the k-arrangement where k varies from 1 to D, where D is the size of the data collections.
This means to compute – my first post I can’t post image so look at equation located at :
In order to do it, you can make k varies, and the for each k may n varies (i.e. deal only with a sub array or data to enumerate the k-permutations). These k-permutations can be found by walking the array to the right and to the left using recursion.
Here is a quick bootstrap that proves to enumerate whart is required :
The program outputs for {A,B,C} :
And for {A,B,C,D} :
Which is not the exhaustive enumeration. Basically we should have:
(my first post I can’t post image to look at equation located in my reply, I don’t have the reputation to post 2 links … #omg)
That makes 64 combinaisons, distributed as follows:
You can see that my program is OK for k=1, k=2, and k=3. But there are not 24 combinaisons for k=4. In order to complete the program, you will need to iterate also on other type of shuffling the data than circular permutation. Actually when k=4, circular permutation does not generate for instance ADBC as input data (hence DBCA cannot be generated by my implementation for instance). In this case, you will want to enumerate all possible data input array with n elements in all possible order. This is a special case of k-permutation, where k=n, and therefore leads to finding the
n!permutation. We can achieve this by calling the EnumUrl method for each of then!possible permutation.For this, you should update
EnumUrl enumerator = new EnumUrl(data);accordingly, but I guess I am letting some work for you to make 🙂HTH