START
ESCWYP
MUSIC
VIDEOS
------------------------------------------------- permut.awk -----------------------
# Give the parameter on STDIN like this:
# echo 4 | awk -f permut.awk
function freespot(ind) {
if(ind >= 0){
f[ind] = 1;
if(ind < minfree){
minfree = ind;
}
}
}
function takespot(ind,i) {
if(ind >= 0){
f[ind] = 0;
if(ind == minfree){
i = 0;
for(i = minfree+1; i < num; ++i){
if(f[i] == 1){
minfree = i;
break;
}
}
}
}
}
function perm () {
count = 1;
while(ind >= 0){
start = c[ind]+1;
if(start == 0){
start = minfree;
}
i = start;
for(i; i < num; ++i){
if(f[i] == 1){
freespot(c[ind]);
takespot(i);
c[ind] = i;
if(ind < num-1){
ind++;
}else{
freespot(i);
j = 0;
if(num > 100){
for(j = 0; j < 10; ++j){
printf("%i ", c[j]);
}
printf("... ");
for(j = num-10; j < num; ++j){
printf("%i ", c[j]);
}
}else{
for(j = 0; j < num; ++j){
printf("%i ", c[j]);
}
}
printf("| %i\n", count);
count++;
}
break;
}
}
if(ind == old){
freespot(c[ind]);
c[ind] = -1;
ind--;
}
old = ind;
}
}
{
if($1 ~ /^[0-9]+$/){
num = $1;
}else{
num = 2;
}
if(num < 2){
num = 2
}
ind = 0;
old = 0;
minfree = 0;
for(i = 0; i < num; ++i){
c[i] = -1;
f[i] = 1;
}
perm();
}
START
ESCWYP
MUSIC
VIDEOS