aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-12-12 21:04:20 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-12-15 17:28:53 +0100
commit32a480a864ef53a82230a306897c0bf991ce539e (patch)
treed395c887c5e64b2dc3308689fb3399eed548efe6
parent41d3978e8fb9d804b0c970188e778fba0c85616e (diff)
downloadperlweeklychallenge-club-32a480a864ef53a82230a306897c0bf991ce539e.tar.gz
perlweeklychallenge-club-32a480a864ef53a82230a306897c0bf991ce539e.tar.bz2
perlweeklychallenge-club-32a480a864ef53a82230a306897c0bf991ce539e.zip
Solution to task 2
-rwxr-xr-xchallenge-247/jo-37/perl/ch-2.pl56
1 files changed, 56 insertions, 0 deletions
diff --git a/challenge-247/jo-37/perl/ch-2.pl b/challenge-247/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..852871a4d2
--- /dev/null
+++ b/challenge-247/jo-37/perl/ch-2.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0 '!float';
+use PDL;
+use PDL::Char;
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV == 1;
+usage: $0 [-examples] [-tests] [S]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+S
+ string of lower case ascii characters
+
+EOS
+
+
+### Input and Output
+
+say most_frequent_pair(shift);
+
+
+### Implementation
+
+sub most_frequent_pair {
+ my $s = PDL::Char->new(shift);
+ my ($count, $pairs) = rlevec($s->lags(0, 1, $s->dim(0) - 1)->qsortvec);
+ $pairs->atstr($count->long->maximum_ind);
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+ is most_frequent_pair('abcdbca'), 'bc', 'example 1';
+ is most_frequent_pair('cdeabeabfcdfabgcd'), 'ab', 'example 2';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ }
+
+ done_testing;
+ exit;
+}