View Javadoc

1   package net.obsearch;
2   
3   import java.util.logging.Logger;
4   
5   import org.junit.Test;
6   public class TestQs {
7   	static Logger logger = Logger.getLogger(TestQs.class.getSimpleName());
8   	
9   	
10  	public static void combiCounter(char[] chars){
11  		long counter = 1;
12  		long end = (long)Math.pow(2, chars.length);
13  		while(counter <= end){
14  			printChars(counter, chars);
15  			counter++;
16  		}
17  	}
18  	
19  	public static void printChars(long counter, char[]chars){
20  		int i = 0;
21  		while(i < chars.length){
22  			if((counter & (1 << i)) != 0){
23  				System.out.print(chars[i]);
24  			}
25  			i++;
26  		}
27  		System.out.println();
28  	}
29  	
30  	public static void combiRecursive(char[] chars){
31  		 combiRecursiveAux(chars, 0, new StringBuilder(), new boolean[chars.length]);
32  	}
33  	
34  	
35  	public static void combiRecursiveAux(char[] chars, int start, StringBuilder buf, boolean[] flags){
36  		int i = start;
37  		while(i < chars.length){
38  			if(flags[i]){
39  				i++;
40  				continue;
41  			}
42  			buf.append(chars[i]);
43  			System.out.println(buf.toString());
44  			flags[i] = true;			
45  			combiRecursiveAux(chars, start, buf, flags);
46  			flags[i] = false;
47  			buf.setLength(buf.length() - 1);				
48  			i++;
49  		}
50  	}
51  	
52  	@Test
53  	public void testCombi(){
54  		//combiCounter(new char []{'w', 'x', 'y', 'z'});
55  		combiRecursive(new char []{'w', 'x', 'y', 'z'});
56  	}
57  	
58  }