Permutations with repetitions: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(→‎Python - Writing a generator: Pylinted for Python 3, updated primitives and output, added {works with})
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 13:
{{Template:Combinations and permutations}}
<br>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68|Revision 1 - one minor extension to language used - PRAGMA READ, similar to C's #include directive.}}
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-2.6 algol68g-2.6].}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
'''File: prelude_permutations_with_repetitions.a68'''<lang algol68># -*- coding: utf-8 -*- #
 
MODE PERMELEMLIST = FLEX[0]PERMELEM;
MODE PERMELEMLISTYIELD = PROC(PERMELEMLIST)VOID;
 
PROC perm gen elemlist = (FLEX[]PERMELEMLIST master, PERMELEMLISTYIELD yield)VOID:(
[LWB master:UPB master]INT counter;
[LWB master:UPB master]PERMELEM out;
FOR i FROM LWB counter TO UPB counter DO
INT c = counter[i] := LWB master[i];
out[i] := master[i][c]
OD;
yield(out);
WHILE TRUE DO
INT next i := LWB counter;
counter[next i] +:= 1;
FOR i FROM LWB counter TO UPB counter WHILE counter[i]>UPB master[i] DO
INT c = counter[i] := LWB master[i];
out[i] := master[i][c];
next i := i + 1;
IF next i > UPB counter THEN done FI;
counter[next i] +:= 1
OD;
INT c = counter[next i];
out[next i] := master[next i][c];
yield(out)
OD;
done: SKIP
);
 
SKIP</lang>'''File: test_permutations_with_repetitions.a68'''<lang algol68>#!/usr/bin/a68g --script #
# -*- coding: utf-8 -*- #
 
MODE PERMELEM = STRING;
PR READ "prelude_permutations_with_repetitions.a68" PR;
 
INT lead actor = 1, co star = 2;
PERMELEMLIST actors list = ("Chris Ciaffa", "Keith Urban","Tom Cruise",
"Katie Holmes","Mimi Rogers","Nicole Kidman");
 
FLEX[0]PERMELEMLIST combination := (actors list, actors list, actors list, actors list);
 
FORMAT partner fmt = $g"; "$;
test:(
# FOR PERMELEMELEM candidate in # perm gen elemlist(combination #) DO (#,
## (PERMELEMLIST candidate)VOID: (
printf((partner fmt,candidate));
IF candidate[lead actor] = "Keith Urban" AND candidate[co star]="Nicole Kidman" OR
candidate[co star] = "Keith Urban" AND candidate[lead actor]="Nicole Kidman" THEN
print((" => Sunday + Faith as extras", new line)); # children #
done
FI;
print(new line)
# OD #));
done: SKIP
)</lang>'''Output:'''
<pre>
Chris Ciaffa; Chris Ciaffa; Chris Ciaffa; Chris Ciaffa;
Keith Urban; Chris Ciaffa; Chris Ciaffa; Chris Ciaffa;
Tom Cruise; Chris Ciaffa; Chris Ciaffa; Chris Ciaffa;
Katie Holmes; Chris Ciaffa; Chris Ciaffa; Chris Ciaffa;
Mimi Rogers; Chris Ciaffa; Chris Ciaffa; Chris Ciaffa;
Nicole Kidman; Chris Ciaffa; Chris Ciaffa; Chris Ciaffa;
Chris Ciaffa; Keith Urban; Chris Ciaffa; Chris Ciaffa;
Keith Urban; Keith Urban; Chris Ciaffa; Chris Ciaffa;
Tom Cruise; Keith Urban; Chris Ciaffa; Chris Ciaffa;
Katie Holmes; Keith Urban; Chris Ciaffa; Chris Ciaffa;
Mimi Rogers; Keith Urban; Chris Ciaffa; Chris Ciaffa;
Nicole Kidman; Keith Urban; Chris Ciaffa; Chris Ciaffa; => Sunday + Faith as extras
</pre>
 
=={{header|AppleScript}}==
Line 285 ⟶ 360:
<pre>Permutation 589 of 1024: CRACK
Found after searching from AAAAA thru ARACK</pre>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68|Revision 1 - one minor extension to language used - PRAGMA READ, similar to C's #include directive.}}
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-2.6 algol68g-2.6].}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
'''File: prelude_permutations_with_repetitions.a68'''<lang algol68># -*- coding: utf-8 -*- #
 
MODE PERMELEMLIST = FLEX[0]PERMELEM;
MODE PERMELEMLISTYIELD = PROC(PERMELEMLIST)VOID;
 
PROC perm gen elemlist = (FLEX[]PERMELEMLIST master, PERMELEMLISTYIELD yield)VOID:(
[LWB master:UPB master]INT counter;
[LWB master:UPB master]PERMELEM out;
FOR i FROM LWB counter TO UPB counter DO
INT c = counter[i] := LWB master[i];
out[i] := master[i][c]
OD;
yield(out);
WHILE TRUE DO
INT next i := LWB counter;
counter[next i] +:= 1;
FOR i FROM LWB counter TO UPB counter WHILE counter[i]>UPB master[i] DO
INT c = counter[i] := LWB master[i];
out[i] := master[i][c];
next i := i + 1;
IF next i > UPB counter THEN done FI;
counter[next i] +:= 1
OD;
INT c = counter[next i];
out[next i] := master[next i][c];
yield(out)
OD;
done: SKIP
);
 
SKIP</lang>'''File: test_permutations_with_repetitions.a68'''<lang algol68>#!/usr/bin/a68g --script #
# -*- coding: utf-8 -*- #
 
MODE PERMELEM = STRING;
PR READ "prelude_permutations_with_repetitions.a68" PR;
 
INT lead actor = 1, co star = 2;
PERMELEMLIST actors list = ("Chris Ciaffa", "Keith Urban","Tom Cruise",
"Katie Holmes","Mimi Rogers","Nicole Kidman");
 
FLEX[0]PERMELEMLIST combination := (actors list, actors list, actors list, actors list);
 
FORMAT partner fmt = $g"; "$;
test:(
# FOR PERMELEMELEM candidate in # perm gen elemlist(combination #) DO (#,
## (PERMELEMLIST candidate)VOID: (
printf((partner fmt,candidate));
IF candidate[lead actor] = "Keith Urban" AND candidate[co star]="Nicole Kidman" OR
candidate[co star] = "Keith Urban" AND candidate[lead actor]="Nicole Kidman" THEN
print((" => Sunday + Faith as extras", new line)); # children #
done
FI;
print(new line)
# OD #));
done: SKIP
)</lang>'''Output:'''
<pre>
Chris Ciaffa; Chris Ciaffa; Chris Ciaffa; Chris Ciaffa;
Keith Urban; Chris Ciaffa; Chris Ciaffa; Chris Ciaffa;
Tom Cruise; Chris Ciaffa; Chris Ciaffa; Chris Ciaffa;
Katie Holmes; Chris Ciaffa; Chris Ciaffa; Chris Ciaffa;
Mimi Rogers; Chris Ciaffa; Chris Ciaffa; Chris Ciaffa;
Nicole Kidman; Chris Ciaffa; Chris Ciaffa; Chris Ciaffa;
Chris Ciaffa; Keith Urban; Chris Ciaffa; Chris Ciaffa;
Keith Urban; Keith Urban; Chris Ciaffa; Chris Ciaffa;
Tom Cruise; Keith Urban; Chris Ciaffa; Chris Ciaffa;
Katie Holmes; Keith Urban; Chris Ciaffa; Chris Ciaffa;
Mimi Rogers; Keith Urban; Chris Ciaffa; Chris Ciaffa;
Nicole Kidman; Keith Urban; Chris Ciaffa; Chris Ciaffa; => Sunday + Faith as extras
</pre>
 
=={{header|AutoHotkey}}==
Line 1,307:
[5, 6, 6]
[6, 6, 6]</pre>
 
=={{header|K}}==
enlist each from x on the left and each from x on the right where x is range 10
Line 1,418 ⟶ 1,419:
<pre>{[1,1],[1,2],[1,3],[2,1],[2,2],[2,3],[3,1],[3,2],[3,3]}</pre>
 
=={{header|Perl}}==
<lang perl>use Algorithm::Combinatorics qw/tuples_with_repetition/;
print join(" ", map { "[@$_]" } tuples_with_repetition([qw/A B C/],2)), "\n";</lang>
{{out}}
<pre>[A A] [A B] [A C] [B A] [B B] [B C] [C A] [C B] [C C]</pre>
 
Solving the crack problem:
<lang perl>use Algorithm::Combinatorics qw/tuples_with_repetition/;
my $iter = tuples_with_repetition([qw/A C K R/], 5);
my $tries = 0;
while (my $p = $iter->next) {
$tries++;
die "Found the combination after $tries tries!\n" if join("",@$p) eq "CRACK";
}</lang>
{{out}}
<pre>Found the combination after 455 tries!</pre>
 
=={{header|Perl 6}}==
 
We can use the <tt>X</tt> operator ("cartesian product") to cross the list with itself.<br>
For <math>n=2</math>:
 
{{works with|rakudo|2016.07}}
<lang perl6>my @k = <a b c>;
 
.say for @k X @k;</lang>
 
For arbitrary <math>n</math>:
 
{{works with|rakudo|2016.07}}
<lang perl6>my @k = <a b c>;
my $n = 2;
 
.say for [X] @k xx $n;</lang>
 
{{out}}
<pre>a a
a b
a c
b a
b b
b c
c a
c b
c c</pre>
 
Here is an other approach, counting all <math>k^n</math> possibilities in base <math>k</math>:
 
{{works with|rakudo|2016.07}}
<lang perl6>my @k = <a b c>;
my $n = 2;
 
say @k[.polymod: +@k xx $n-1] for ^@k**$n</lang>
 
{{out}}
<pre>a a
b a
c a
a b
b b
c b
a c
b c
c c</pre>
=={{header|Pascal}}==
{{works with|Free Pascal}}
Line 1,602 ⟶ 1,539:
//"old" compiler-version
//real 0m3.465s /fpc/2.6.4/ppc386 "%f" -al -Xs -XX -O3</pre>
 
=={{header|Perl}}==
<lang perl>use Algorithm::Combinatorics qw/tuples_with_repetition/;
print join(" ", map { "[@$_]" } tuples_with_repetition([qw/A B C/],2)), "\n";</lang>
{{out}}
<pre>[A A] [A B] [A C] [B A] [B B] [B C] [C A] [C B] [C C]</pre>
 
Solving the crack problem:
<lang perl>use Algorithm::Combinatorics qw/tuples_with_repetition/;
my $iter = tuples_with_repetition([qw/A C K R/], 5);
my $tries = 0;
while (my $p = $iter->next) {
$tries++;
die "Found the combination after $tries tries!\n" if join("",@$p) eq "CRACK";
}</lang>
{{out}}
<pre>Found the combination after 455 tries!</pre>
 
=={{header|Phix}}==
Line 2,066 ⟶ 2,020:
<pre>'((1 1) (1 2) (1 3) (2 1) (2 2) (2 3) (3 1) (3 2) (3 3))
'((1 1) (1 2) (1 3) (2 1) (2 2) (2 3) (3 1) (3 2) (3 3))</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
 
We can use the <tt>X</tt> operator ("cartesian product") to cross the list with itself.<br>
For <math>n=2</math>:
 
{{works with|rakudo|2016.07}}
<lang perl6>my @k = <a b c>;
 
.say for @k X @k;</lang>
 
For arbitrary <math>n</math>:
 
{{works with|rakudo|2016.07}}
<lang perl6>my @k = <a b c>;
my $n = 2;
 
.say for [X] @k xx $n;</lang>
 
{{out}}
<pre>a a
a b
a c
b a
b b
b c
c a
c b
c c</pre>
 
Here is an other approach, counting all <math>k^n</math> possibilities in base <math>k</math>:
 
{{works with|rakudo|2016.07}}
<lang perl6>my @k = <a b c>;
my $n = 2;
 
say @k[.polymod: +@k xx $n-1] for ^@k**$n</lang>
 
{{out}}
<pre>a a
b a
c a
a b
b b
c b
a c
b c
c c</pre>
 
=={{header|REXX}}==
10,333

edits