diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-02-03 11:16:19 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-03 11:16:19 +0000 |
| commit | 0247be1ea51c63d388e3e2c8c8596b2b1500d416 (patch) | |
| tree | b9fc67f15cb1118fe9f44626b5843d3f6e4ecdd4 | |
| parent | b700b52fba41b64aaae9e234d3f56f0f2817806a (diff) | |
| parent | ee4d7a4229b9cb13650429550bf292b22f8e5757 (diff) | |
| download | perlweeklychallenge-club-0247be1ea51c63d388e3e2c8c8596b2b1500d416.tar.gz perlweeklychallenge-club-0247be1ea51c63d388e3e2c8c8596b2b1500d416.tar.bz2 perlweeklychallenge-club-0247be1ea51c63d388e3e2c8c8596b2b1500d416.zip | |
Merge pull request #11524 from choroba/ech307
Add solutions to 307: Check Order & Find Anagrams by E. Choroba
| -rwxr-xr-x | challenge-307/e-choroba/perl/ch-1.pl | 16 | ||||
| -rwxr-xr-x | challenge-307/e-choroba/perl/ch-2.pl | 22 |
2 files changed, 38 insertions, 0 deletions
diff --git a/challenge-307/e-choroba/perl/ch-1.pl b/challenge-307/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..9a4f1e113d --- /dev/null +++ b/challenge-307/e-choroba/perl/ch-1.pl @@ -0,0 +1,16 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub check_order(@ints) { + my @sorted = sort { $a <=> $b } @ints; + return [grep $ints[$_] != $sorted[$_], 0 .. $#ints] +} + +use Test2::V0; +plan(3); + +is check_order(5, 2, 4, 3, 1), bag { item $_ for 0, 2, 3, 4; end }, 'Example 1'; +is check_order(1, 2, 1, 1, 3), bag { item $_ for 1, 3; end }, 'Example 2'; +is check_order(3, 1, 3, 2, 3), bag { item $_ for 0, 1, 3; end }, 'Example 3'; diff --git a/challenge-307/e-choroba/perl/ch-2.pl b/challenge-307/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..a2d70f1b9e --- /dev/null +++ b/challenge-307/e-choroba/perl/ch-2.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub find_anagrams(@words) { + my @sorted = map join("", sort split //), @words; + my $different = 1; + for my $i (1 .. $#sorted) { + ++$different if $sorted[ $i - 1 ] ne $sorted[$i]; + } + return $different +} + +use Test::More tests => 2 + 3; + +is find_anagrams('acca', 'dog', 'god', 'perl', 'repl'), 3, 'Example 1'; +is find_anagrams('abba', 'baba', 'aabb', 'ab', 'ab'), 2, 'Example 2'; + +is find_anagrams('ab'), 1, 'Single word'; +is find_anagrams('ab', 'cd'), 2, 'No anagrams'; +is find_anagrams('ab', 'cd', 'ba'), 3, 'Separated anagrams'; |
