1
2
3
4
5
6
7
@woerter = qw / Perl Kamel Larry foo /;
$zufall = int(rand(3)) + 1;
$wort = $woerter[$zufall] ;
@Split = split ("",$wort);
foreach (@Split) {
print " $_ ";
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#!/usr/bin/perl use warnings; use strict; use List::Util 'shuffle'; my $a = "KAMEL"; my @b = split("", $a); my %c; my $x = 0; my $n = 0; while ($x < 1000) { @b = shuffle(@b); my $s = join("", @b); if (exists($c{$s})) { $x++; } else { $c{$s} = ""; $x = 0; $n++; } } my @e = sort(keys(%c)); foreach my $i (@e) { print "$i\n"; } print "$n permutations, probably all.\n";
1 2 3
use Algorithm::Permute; my @array = split//,"TEST"; Algorithm::Permute::permute { say @array } @array;
2013-09-22T19:58:01 RaubtierAber wozu sollte man schon alle Permutationen erzeugen wollen, wenn man doch leicht testen kann, ob ein Wort eine Permutation eines anderen Wortes ist.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#!/usr/bin/perl use warnings; use strict; sub isAnagram { my $w1 = shift; my $w2 = shift; if (length($w1) != length($w2)) { return 0; } my %h1; my %h2; my @a1 = split("", $w1); my @a2 = split("", $w2); my $i; foreach $i (@a1) { $h1{$i} += 1; } foreach $i (@a2) { if (! exists($h1{$i})) { return 0; } $h2{$i} += 1; } foreach $i (keys(%h1)) { if (! exists($h2{$i})) { return 0; } print "$i\t"; print "$h1{$i}\t"; print "$h2{$i}\n"; if ($h1{$i} != $h2{$i}) { return 0; } } return 1; } print isAnagram("Hallo", "ollaH") . "\n\n"; print isAnagram("KAMEL", "AMEK") . "\n\n"; print isAnagram("KAMEL", "LLMEK") . "\n\n"; print isAnagram("KAMEL", "LAMEK") . "\n"; print isAnagram("DONAUDAMPFSCHIFFAHRTSGESELLSCHAFTSKAPITÄN", "RGHEKAALPFOPMTEFSACDTUFSNINSCTFDAÄSHLSAHI") . "\n";