![複数のランダムな単語ペアの組み合わせを作成する[閉じる]](https://linux33.com/image/161941/%E8%A4%87%E6%95%B0%E3%81%AE%E3%83%A9%E3%83%B3%E3%83%80%E3%83%A0%E3%81%AA%E5%8D%98%E8%AA%9E%E3%83%9A%E3%82%A2%E3%81%AE%E7%B5%84%E3%81%BF%E5%90%88%E3%82%8F%E3%81%9B%E3%82%92%E4%BD%9C%E6%88%90%E3%81%99%E3%82%8B%5B%E9%96%89%E3%81%98%E3%82%8B%5D.png)
ファイル1には、ランダムにペアにする必要がある単語のリストが含まれています。
Tiger
Cat
Fish
Frog
Dog
Mouse
Elephant
Monkey
ファイル2には、ランダムペアリング中に(すべてのソリューションで)使用してはならないペアが含まれています。
Elephant-Dog
Cat-Fish
Monkey-Frog
Dog Elephant、Fish Cat、Frog MonkeyもペアがFile2に表示されるため(方向に関係なく)削除する必要があります。合計6つのソリューションが必要で、各ソリューションには最大5対必要です。 Tiger-CatとCat-Tigerは同じと見なされ、ソリューションに一緒に表示される場合は削除する必要があります。同じペア(たとえば、カエル - 犬)が複数のソリューションに表示されることがあります。
出力は次のとおりです(ここには1つの解決策しかありません)。
Tiger-Cat
Cat-Dog
Monkey-Cat
Frog-Dog
Elephant-Cat
答え1
bash
解決策:
for i in {1..6}; do
printf '==== solution %d ====\n' "$i"
# initialize solution
solution=()
while [ ${#solution[@]} -lt 5 ]; do
# select two random lines from file1
w1=$(shuf -n 1 file1)
w2=$(shuf -n 1 file1)
# skip if word1 is the same as word2
[ "$w1" == "$w2" ] && continue
# skip if pair exists in same solution or is not allowed from file2
cat <(printf '%s\n' "${solution[@]}") file2 | grep -qx "$w1-$w2" && continue
cat <(printf '%s\n' "${solution[@]}") file2 | grep -qx "$w2-$w1" && continue
# output
solution+=("${w1}-${w2}")
done
printf '%s\n' "${solution[@]}"
done
出力:
==== solution 1 ====
Fish-Monkey
Elephant-Mouse
Dog-Tiger
Mouse-Fish
Dog-Cat
==== solution 2 ====
Cat-Frog
Elephant-Monkey
Cat-Mouse
Tiger-Elephant
Fish-Tiger
==== solution 3 ====
Cat-Frog
Tiger-Monkey
Frog-Elephant
Dog-Fish
Elephant-Cat
==== solution 4 ====
Cat-Dog
Mouse-Elephant
Monkey-Elephant
Cat-Monkey
Tiger-Cat
==== solution 5 ====
Tiger-Monkey
Tiger-Cat
Mouse-Monkey
Mouse-Fish
Monkey-Cat
==== solution 6 ====
Monkey-Mouse
Dog-Monkey
Monkey-Fish
Tiger-Elephant
Cat-Tiger
答え2
パールソリューション。
#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };
use List::Util qw{ shuffle };
open my $fh_list, '<', shift or die $!;
chomp( my @words = sort <$fh_list> );
open my $fh_ban, '<', shift or die $!;
my %ban;
while (<$fh_ban>) {
chomp;
my ($ban1, $ban2) = sort split /-/;
undef $ban{"$ban1-$ban2"};
}
my @all;
for my $i1 (0 .. $#words) {
for my $i2 ($i1 + 1 .. $#words) {
my $pair = [ $i1, $i2 ];
push @all, $pair unless exists $ban{"$words[$i1]-$words[$i2]"};
}
}
my @solutions;
my %used;
while (@solutions < 6) {
my $solution = join ' ', sort +(shuffle(0 .. $#all))[0 .. 4];
redo if exists $used{$solution};
undef $used{$solution};
push @solutions, [
map join('-', @words[
@{ $all[$_] }[int rand 2 ? (0, 1) : (1, 0)]
]), split ' ', $solution
];
}
say join "\n", @$_, '---' for @solutions;
まず、単語を配列として読み、禁止された単語のペアをハッシュとして読みます。次に、第1の要素が第2の要素の前に整列される可能性のある全ての組み合わせを生成する。次に、可能なすべてのペアを混ぜ合わせ、6つの異なるソリューションが出るまで上位5つを選択します。 「@all」配列には要素のインデックスのみが含まれており、その要素を出力するときにランダムに混在するため、「Cat-Dog」と「Dog-Cat」の両方が得られます。
出力例:
Elephant-Cat
Monkey-Cat
Fish-Elephant
Monkey-Fish
Frog-Tiger
---
Tiger-Cat
Tiger-Fish
Fish-Elephant
Dog-Monkey
Cat-Frog
---
Tiger-Fish
Cat-Elephant
Elephant-Frog
Mouse-Elephant
Frog-Cat
---
Tiger-Fish
Mouse-Fish
Monkey-Fish
Frog-Tiger
Cat-Dog
---
Dog-Frog
Elephant-Frog
Dog-Tiger
Tiger-Mouse
Tiger-Monkey
---
Tiger-Cat
Elephant-Frog
Tiger-Mouse
Cat-Frog
Cat-Dog
---