diff options
| -rw-r--r-- | challenge-327/zapwai/perl/ch-1.pl | 16 | ||||
| -rw-r--r-- | challenge-327/zapwai/perl/ch-2.pl | 40 |
2 files changed, 56 insertions, 0 deletions
diff --git a/challenge-327/zapwai/perl/ch-1.pl b/challenge-327/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..a5a189975e --- /dev/null +++ b/challenge-327/zapwai/perl/ch-1.pl @@ -0,0 +1,16 @@ +use v5.38; + +sub proc(@ints) { + say "Input: @ints"; + my @o = grep {!($_ ~~ @ints)} (1 .. @ints); + say "Output: @o"; +} + +my @ints = (1,2,1,3,2,5); +proc(@ints); +@ints = (1,1,1); +proc(@ints); +@ints = (2,2,1); +proc(@ints); +@ints = (2,1); +proc(@ints); diff --git a/challenge-327/zapwai/perl/ch-2.pl b/challenge-327/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..01d03cd59c --- /dev/null +++ b/challenge-327/zapwai/perl/ch-2.pl @@ -0,0 +1,40 @@ +use v5.38; + +sub mad(@ints) { + my $min = abs($ints[1] - $ints[0]); + for my $i (0 .. $#ints - 1) { + for my $j ($i + 1 .. $#ints) { + my $diff = abs($ints[$i] - $ints[$j]); + $min = $diff if ($min > $diff); + } + } + my @out; + for my $i (0 .. $#ints - 1) { + for my $j ($i + 1 .. $#ints) { + my $diff = abs($ints[$i] - $ints[$j]); + if ($diff == $min) { + if ($ints[$i] < $ints[$j]) { + push @out, [$ints[$i], $ints[$j]]; + } else { + push @out, [$ints[$j], $ints[$i]]; + } + } + } + } + return @out; +} + +sub proc(@ints) { + say "Input: @ints"; + my @o = mad(@ints); + print "Output: "; + print "(". join(", ", @$_) . ") " for (@o); + say ""; +} + +my @ints = (4,1,2,3); +proc(@ints); +@ints = (1,3,7,11,15); +proc(@ints); +@ints = (1,5,3,8); +proc(@ints); |
