4

Consider the following string

abcd

I can return 2 character permutations (cartesian product) like this

$ echo {a,b,c,d}{a,b,c,d}
aa ab ac ad ba bb bc bd ca cb cc cd da db dc dd

However I would like to remove redundant entries such as

ba ca cb da db dc

and invalid entries

aa bb cc dd

so I am left with

ab ac ad bc bd cd

Example

Zombo
  • 1
  • 62
  • 391
  • 407

5 Answers5

6

Here's a pure bash one:

#!/bin/bash

pool=( {a..d} )
for((i=0;i<${#pool[@]}-1;++i)); do
    for((j=i+1;j<${#pool[@]};++j)); do
        printf '%s\n' "${pool[i]}${pool[j]}"
    done
 done

and another one:

#!/bin/bash

pool=( {a..d} )
while ((${#pool[@]}>1)); do
    h=${pool[0]}
    pool=("${pool[@]:1}")
    printf '%s\n' "${pool[@]/#/$h}"
done

They can be written as functions (or scripts):

get_perms_ordered() {
    local i j
    for((i=1;i<"$#";++i)); do
        for((j=i+1;j<="$#";++j)); do
            printf '%s\n' "${!i}${!j}"
        done
     done
}

or

get_perms_ordered() {
    local h
    while (("$#">1)); do
        h=$1; shift
        printf '%s\n' "${@/#/$h}"
    done
}

Use as:

$ get_perms_ordered {a..d}
ab
ac
ad
bc
bd
cd

This last one can easily be transformed into a recursive function to obtain ordered permutations of a given length (without replacement—I'm using the silly ball-urn probability vocabulary), e.g.,

get_withdraws_without_replacement() {
    # $1=number of balls to withdraw
    # $2,... are the ball "colors"
    # return is in array gwwr_ret
    local n=$1 h r=()
    shift
    ((n>0)) || return
    ((n==1)) && { gwwr_ret=( "$@" ); return; }
    while (("$#">=n)); do
        h=$1; shift
        get_withdraws_without_replacement "$((n-1))" "$@"
        r+=( "${gwwr_ret[@]/#/$h}" )
    done
    gwwr_ret=( "${r[@]}" )
}

Then:

$ get_withdraws_without_replacement 3 {a..d}
$ echo "${gwwr_ret[@]}"
abc abd acd bcd
Zombo
  • 1
  • 62
  • 391
  • 407
gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
4

You can use awk to filter away the entries you don't want:

echo {a,b,c,d}{a,b,c,d} | awk -v FS="" -v RS=" " '$1 == $2 { next } ; $1 > $2 { SEEN[ $2$1 ] = 1 ; next } ; { SEEN[ $1$2 ] =1 } ; END { for ( I in SEEN ) { print I } }'

In details:

echo {a,b,c,d}{a,b,c,d} \
| awk -v FS="" -v RS=" " '

   # Ignore identical values
   $1 == $2 { next }

   # Reorder and record inverted entries
   $1 > $2  { SEEN[ $2$1 ] = 1 ; next }

            # Record everything else
            { SEEN[ $1$2 ] = 1 }

   # Print the final list
   END      { for ( I in SEEN ) { print I } }
 '

FS="" tells awk that each character is a separate field. RS=" " uses spaces to separate records.

1

I'm sure someone's going to do this in one line of awk, but here is something in bash:

#!/bin/bash
seen=":"
result=""

for i in "$@"
do
    for j in "$@"
    do
        if [ "$i" != "$j" ]
        then
            if [[ $seen != *":$j$i:"* ]]
            then
                result="$result $i$j"
                seen="$seen$i$j:"
            fi
        fi
    done
done
echo $result

Output:

$ ./prod.sh a b c d
ab ac ad bc bd cd

$ ./prod.sh I have no life
Ihave Ino Ilife haveno havelife nolife
John C
  • 4,276
  • 2
  • 17
  • 28
0

here is a pseudo code to achieve that, based on your restrictions, and using an array for your characters:

for (i=0;i<array.length;i++)
{
    for (j=i+1;j<array.length;j++)
    {
        print array[i] + array[j]; //concatenation
    }
}
luisluix
  • 547
  • 5
  • 17
0

I realized that I am not looking for permutations, but the power set. Here is an implementation in Awk:

{
  for (c = 0; c < 2 ^ NF; c++) {
    e = 0
    for (d = 0; d < NF; d++)
      if (int(c / 2 ^ d) % 2) {
        printf "%s", $(d + 1)
      }
    print ""
  }
}

Input:

a b c d

Output:

a
b
ab
c
ac
bc
abc
d
ad
bd
abd
cd
acd
bcd
abcd

Example

Zombo
  • 1
  • 62
  • 391
  • 407