Skip to content

Commit 2f4149f

Browse files
committed
New Perl program
1 parent d7f4681 commit 2f4149f

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

perl/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Programs in this folder can be run from the command line like so:
1010
perl triple.pl
1111
```
1212

13+
```
14+
perl permutations.pl I like carrots
15+
```
16+
1317
```
1418
perl top_ten_scorers.pl < ../test/wnba_input
1519
```
@@ -18,6 +22,7 @@ To run the tests on a Unix-like shell:
1822

1923
```
2024
./test.sh
25+
2126
```
2227

2328
## About Perl

perl/permutations.pl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use strict;
2+
use warnings;
3+
4+
sub print_permutations {
5+
my ($a, $n) = @_;
6+
if ($n <= 0) {
7+
print join("\t", @$a), "\n";
8+
} else {
9+
for my $i (0 .. $n - 1) {
10+
print_permutations($a, $n - 1);
11+
my $j = $n % 2 == 0 ? 0 : $i;
12+
@$a[$j, $n] = @$a[$n, $j];
13+
}
14+
print_permutations($a, $n - 1);
15+
}
16+
}
17+
18+
print_permutations(\@ARGV, scalar(@ARGV) - 1);

0 commit comments

Comments
 (0)