diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-05-13 13:15:06 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-13 13:15:06 +0100 |
| commit | 4ce7dd1af1d512e1aaa9f56ab670c2df336615f7 (patch) | |
| tree | 6636663e45e53cd7a8754812c47912f3311654b2 /challenge-321 | |
| parent | c3a1d8ae574ba0d5f9a407e83205160bac841046 (diff) | |
| parent | a721749c255eade8833d8a4db799790bc49f9691 (diff) | |
| download | perlweeklychallenge-club-4ce7dd1af1d512e1aaa9f56ab670c2df336615f7.tar.gz perlweeklychallenge-club-4ce7dd1af1d512e1aaa9f56ab670c2df336615f7.tar.bz2 perlweeklychallenge-club-4ce7dd1af1d512e1aaa9f56ab670c2df336615f7.zip | |
Merge pull request #12011 from choroba/ech321
Solve 321: Distinct Average & Backspace Compare by E. Choroba
Diffstat (limited to 'challenge-321')
| -rwxr-xr-x | challenge-321/e-choroba/perl/ch-1.pl | 17 | ||||
| -rwxr-xr-x | challenge-321/e-choroba/perl/ch-2.pl | 18 |
2 files changed, 35 insertions, 0 deletions
diff --git a/challenge-321/e-choroba/perl/ch-1.pl b/challenge-321/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..6cbf0aa7da --- /dev/null +++ b/challenge-321/e-choroba/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub distinct_average(@nums) { + @nums = sort { $a <=> $b } @nums; + my %avg; + undef $avg{ ($nums[$_] + $nums[ $#nums - $_ ]) / 2 } for 0 .. @nums / 2; + return scalar keys %avg +} + +use Test::More tests => 3; + +is distinct_average(1, 2, 4, 3, 5, 6), 1, 'Example 1'; +is distinct_average(0, 2, 4, 8, 3, 5), 2, 'Example 2'; +is distinct_average(7, 3, 1, 0, 5, 9), 2, 'Example 3'; diff --git a/challenge-321/e-choroba/perl/ch-2.pl b/challenge-321/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..a1603b42c7 --- /dev/null +++ b/challenge-321/e-choroba/perl/ch-2.pl @@ -0,0 +1,18 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub backspace_compare($str1, $str2) { + for ($str1, $str2) { + 1 while s/[^#]#//; + } + return $str1 eq $str2 +} + +use Test2::V0; +plan(3); + +is backspace_compare('ab#c', 'ad#c'), bool(1), 'Example 1'; +is backspace_compare('ab##', 'a#b#'), bool(1), 'Example 2'; +is backspace_compare('a#b', 'c'), bool(0), 'Example 3'; |
