Skip to content

Instantly share code, notes, and snippets.

@Hulzenga
Created July 24, 2015 19:18
Show Gist options
  • Save Hulzenga/f53f648cdc75e13d2c99 to your computer and use it in GitHub Desktop.
Save Hulzenga/f53f648cdc75e13d2c99 to your computer and use it in GitHub Desktop.
import java.util.List;
import java.util.Arrays;
class Count {
public static void main(String[] args) {
List<String> test = Arrays.asList(new String[]{"0", "1", "2"});
count(test, 3);
}
/**
* prints all possible permutations of "s" in incremental order
* @param s ordered list of elements (incremental order)
* @param l of the permutations
*/
public static void count(List<String> s, int l) {
int[] counter = new int[l];
int n = s.size();
OUTER:
while(true) {
print(s, counter);
for (int i = l - 1; ++counter[i] == n; counter[i--] = 0) {
if (i == 0) {
break OUTER;
}
}
}
System.out.println("DONE");
}
public static void print(List<String> s, int[] counter) {
//build output
StringBuilder sb = new StringBuilder();
for (int i = 0; i < counter.length; i++) {
sb.append(s.get(counter[i]));
}
sb.append(System.lineSeparator());
System.out.println(sb);
try {
Thread.sleep(200);
} catch (Exception e) {}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment