From e806b77af6e891e0e75be91a787c7d896ccfed2e Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 12 May 2025 12:16:14 -0400 Subject: Week 321 --- challenge-321/zapwai/perl/ch-1.pl | 22 ++++++++++++++++++++++ challenge-321/zapwai/perl/ch-2.pl | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 challenge-321/zapwai/perl/ch-1.pl create mode 100644 challenge-321/zapwai/perl/ch-2.pl diff --git a/challenge-321/zapwai/perl/ch-1.pl b/challenge-321/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..1c6fd48c24 --- /dev/null +++ b/challenge-321/zapwai/perl/ch-1.pl @@ -0,0 +1,22 @@ +use feature qw(signatures say); + +sub proc(@n) { + say "Input: \@n = @n"; + @n = sort {$a <=> $b} @n; + my @dist; + do { + my $min = shift @n; + my $max = pop @n; + my $avg = ($max + $min) / 2; + push @dist, $avg unless (grep {$_ == $avg} @dist); + last if (@n == 1); + } while (@n); + say "Output: ", scalar @dist; +} + +my @nums = (1, 2, 4, 3, 5, 6); +proc(@nums); +@nums = (0, 2, 4, 8, 3, 5); +proc(@nums); +@nums = (7, 3, 1, 0, 5, 9); +proc(@nums); diff --git a/challenge-321/zapwai/perl/ch-2.pl b/challenge-321/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..5842417135 --- /dev/null +++ b/challenge-321/zapwai/perl/ch-2.pl @@ -0,0 +1,33 @@ +use feature qw(signatures say); + +sub p($s) { + my @d = split '', $s; + my @o; + for (@d) { + if ($_ eq '#') { + pop @o; + } else { + push @o, $_; + } + } + return join '', @o; +} + +sub proc($str1, $str2) { + say "Input: $str1, $str2"; + my $s1 = p($str1); + my $s2 = p($str2); + say "Output: ", ($s1 eq $s2) ? 'true' : 'false'; +} + +$str1 = "ab#c"; +$str2 = "ad#c"; +proc($str1, $str2); + +$str1 = "ab##"; +$str2 = "a#b#"; +proc($str1, $str2); + +$str1 = "a#b"; +$str2 = "c"; +proc($str1, $str2); -- cgit