生成独特的组合

问题描述 投票:0回答:1
#!/bin/bash

# Define the list of letters
letters=("a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m")

# Define the output file
output_file="5_letter_subsets_sorted_unique.txt"

# Generate all 5-letter subsets without repetition
for a in "${letters[@]}"; do
    for b in "${letters[@]}"; do
        for c in "${letters[@]}"; do
            for d in "${letters[@]}"; do
                for e in "${letters[@]}"; do
                    # Ensure no repetition in the subset
                    if [ "$a" != "$b" ] && [ "$a" != "$c" ] && [ "$a" != "$d" ] && [ "$a" != "$e" ] && \
                       [ "$b" != "$c" ] && [ "$b" != "$d" ] && [ "$b" != "$e" ] && \
                       [ "$c" != "$d" ] && [ "$c" != "$e" ] && \
                       [ "$d" != "$e" ]; then
                        subset=$(echo "$a$b$c$d$e" | grep -o . | sort | tr -d '\n' | uniq)
                        echo "$subset" >> "$output_file"
                    fi
                done
            done
        done
    done
done

echo "5-letter subsets (sorted and unique) generated and saved in $output_file"

上述程序从这组字母中生成 5 个字母的所有唯一组合 letter=("a" "b" "c" "d" "e" "f" "g" "h" "i" "j" " k" "l" "m"),不重复,每个组合按字母顺序排序。

问题 - 当前输出中有重复

我已使用 uniq 从输出中删除所有重复项。

电流输出

abcde
abcdf
abcdg
abcdh
abcdi
abcdj
abcdk
abcdl
abcdm
abcde
abcef
abceg
abceh
abcei
abcej
abcek
abcel
abcem
abcef
.
.
.
ijklm

预期产出

abcde
abcdf
abcdg
abcdh
abcdi
abcdj
abcdk
abcdl
abcdm
abcef
abceg
abceh
abcei
abcej
abcek
abcel
abcem
.
.
.
ijklm
shell sorting sh combinations unique
1个回答
0
投票

TXR Lisp 中:

(flow (range "a" "m")
  (perm @1 5)
  (mapcar [chain sort join]))
  (file-put-lines "5_letter_subsets_sorted_unique.txt"))
© www.soinside.com 2019 - 2024. All rights reserved.