diff options
| author | E. Choroba <choroba@matfyz.cz> | 2025-02-03 11:11:57 +0100 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2025-02-03 11:11:57 +0100 |
| commit | ee4d7a4229b9cb13650429550bf292b22f8e5757 (patch) | |
| tree | 19cb2d73af4ae7920e220e3d62e319a682017efb | |
| parent | 75cad926933996bff61b6684c63344fddbb4ea79 (diff) | |
| download | perlweeklychallenge-club-ee4d7a4229b9cb13650429550bf292b22f8e5757.tar.gz perlweeklychallenge-club-ee4d7a4229b9cb13650429550bf292b22f8e5757.tar.bz2 perlweeklychallenge-club-ee4d7a4229b9cb13650429550bf292b22f8e5757.zip | |
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'; |
