Custom Combination and permutation program

package com.kartik;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
 *
 * @author kartik
 * Blog www.kartikchandramandal.blogspot.com
 * One of the best way to do permutation of a data set is to by applying permutation , it help making all combination of a specified length
 */
public class Permutations<T> {
public static void main(String args[]) {
System.out.println("Integer permutation below:");
//Integer permutation
Permutations<Integer> obj = new Permutations<Integer>();
Collection<Integer> input = new ArrayList<Integer>();
input.add(1);
input.add(2);
input.add(3);
Collection<List<Integer>> output = obj.permute(input);
int k = 0;
Set<List<Integer>> pnr = null;
for (int i = 0; i <= input.size(); i++) {
pnr = new HashSet<List<Integer>>();
for(List<Integer> integers : output){
pnr.add(integers.subList(i, integers.size()));
}
k = input.size()- i;
System.out.println("P("+input.size()+","+k+") :"+ "Count ("+pnr.size()+") :- "+pnr);
}
System.out.println("Character permutation and combination below:");
 //String permutation
Permutations<String> objStr = new Permutations<String>();
Collection<String> inputStr = new ArrayList<String>();
inputStr.add("K");
inputStr.add("A");
inputStr.add("R");
inputStr.add("T");
/* inputStr.add("I");*/
Collection<List<String>> outputStr = objStr.permute(inputStr);
int len = 0;
Set<List<String>> pnrStr = null;
for (int i = 0; i <= inputStr.size(); i++) {
pnrStr = new HashSet<List<String>>();
for(List<String> integers : outputStr){
pnrStr.add(integers.subList(i, integers.size()));
}
 len = input.size()- i;
if(i==0){
 System.out.println("P("+inputStr.size()+","+len+") :"+ "Count ("+pnrStr.size()+") :- "+pnrStr);
}
}
}
 public Collection<List<T>> permute(Collection<T> input) {
 Collection<List<T>> output = new ArrayList<List<T>>();
 if (input.isEmpty()) {
 output.add(new ArrayList<T>());
 return output;
}
 List<T> list = new ArrayList<T>(input);
 T head = list.get(0);
 List<T> rest = list.subList(1, list.size());
 for (List<T> permutations : permute(rest)) {
 List<List<T>> subLists = new ArrayList<List<T>>();
 for (int i = 0; i <= permutations.size(); i++) {
 List<T> subList = new ArrayList<T>();
 subList.addAll(permutations);
 subList.add(i, head);
 subLists.add(subList);
}
output.addAll(subLists);
}
 return output;
}
}
Output:
Integer permutation below:
P(3,3) :Count (6) :- [[1, 2, 3], [2, 3, 1], [3, 2, 1], [3, 1, 2], [2, 1, 3], [1, 3, 2]]
P(3,2) :Count (6) :- [[3, 1], [2, 1], [3, 2], [1, 3], [2, 3], [1, 2]]
P(3,1) :Count (3) :- [[3], [1], [2]]
P(3,0) :Count (1) :- [[]]
Character permutation and combination below:
P(4,3) :Count (24) :- [[R, T, A, K], [R, K, T, A], [T, A, R, K], [T, R, K, A], [A, T, R, K], [A, T, K, R], [R, A, T, K], [T, R, A, K], [R, A, K, T], [R, T, K, A], [K, T, A, R], [A, R, T, K], [A, K, R, T], [A, R, K, T], [T, K, R, A], [K, A, T, R], [K, R, T, A], [T, A, K, R], [T, K, A, R], [K, R, A, T], [A, K, T, R], [K, A, R, T], [K, T, R, A], [R, K, A, T]]



Previous
Next Post »

1 comments:

Click here for comments
3 July 2015 at 06:22 ×

Hi Kartik, It is good that you share java code and solutions with others. keep it up.
One advice to you that use some good font so it will be visible.

Congrats bro amit singh rana you got PERTAMAX...! hehehehe...
Reply
avatar