aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-08-30 17:09:48 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-08-30 17:09:48 +0200
commitea70acee29029af7cdb6a53e0d134b1ed83f7657 (patch)
treee1592155eee46a50acbf991a1e26236a3b7acd51
parent9d8b35adb9460155304b6fbaf2b2480c6c7fe723 (diff)
parent327916e161d224f00e8e61c43eeb7f4b868b0b93 (diff)
downloadperlweeklychallenge-club-ea70acee29029af7cdb6a53e0d134b1ed83f7657.tar.gz
perlweeklychallenge-club-ea70acee29029af7cdb6a53e0d134b1ed83f7657.tar.bz2
perlweeklychallenge-club-ea70acee29029af7cdb6a53e0d134b1ed83f7657.zip
Solutions to challenge 284
-rw-r--r--challenge-284/jo-37/blog.txt1
-rwxr-xr-xchallenge-284/jo-37/perl/ch-1.pl66
-rwxr-xr-xchallenge-284/jo-37/perl/ch-2.pl89
3 files changed, 156 insertions, 0 deletions
diff --git a/challenge-284/jo-37/blog.txt b/challenge-284/jo-37/blog.txt
new file mode 100644
index 0000000000..312df3802f
--- /dev/null
+++ b/challenge-284/jo-37/blog.txt
@@ -0,0 +1 @@
+https://github.sommrey.de/the-bears-den/2024/08/30/ch-284.html
diff --git a/challenge-284/jo-37/perl/ch-1.pl b/challenge-284/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..ceab3d9239
--- /dev/null
+++ b/challenge-284/jo-37/perl/ch-1.pl
@@ -0,0 +1,66 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0 '!float';
+use PDL '2.017';
+use PDL::NiceSlice;
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [N]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+N...
+ list of integers
+
+EOS
+
+
+### Input and Output
+
+say lucky_integer(@ARGV);
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2024/08/30/ch-284.html#task-1
+
+
+sub lucky_integer {
+ my ($freq, $val) = rle long(@_)->qsort;
+ my $lucky = $val->where($freq == $val);
+ $lucky->isempty ? -1 : $lucky(-1;-);
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is lucky_integer(2, 2, 3, 4), 2, 'example 1';
+ is lucky_integer(1, 2, 2, 3, 3, 3), 3, 'example 2';
+ is lucky_integer(1, 1, 1, 3), -1, 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is lucky_integer(
+ qw(11 1 11 2 11 2 11 3 11 3 11 3 11 4 11 4 11 4 11 4 11)), 11,
+ 'these go to eleven';
+ }
+
+ done_testing;
+ exit;
+}
diff --git a/challenge-284/jo-37/perl/ch-2.pl b/challenge-284/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..5536c97cbc
--- /dev/null
+++ b/challenge-284/jo-37/perl/ch-2.pl
@@ -0,0 +1,89 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+use experimental 'signatures';
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV == 2;
+usage: $0 [-examples] [-tests] [N1,N2,... M1,M2,...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+N1,N2,...
+ numbers in \@list1, comma and/or space separated
+
+M1,M2,...
+ numbers in \@list2, comma and/or space separated
+
+EOS
+
+
+### Input and Output
+
+say "(@{relative_sort(map [split /[, ] */, $_], @ARGV)})";
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2024/08/30/ch-284.html#task-2
+
+sub relative_sort ($list1, $list2) {
+ (\my %list2)->@{@$list2} = ();
+ my @part2;
+ for my $n (@$list1) {
+ if (exists $list2{$n}) {
+ $list2{$n}++;
+ } else {
+ push @part2, $n;
+ }
+ }
+
+ [
+ (map +($_) x $list2{$_}, @$list2),
+ (sort {$a <=> $b} @part2),
+ ];
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is relative_sort( [2, 3, 9, 3, 1, 4, 6, 7, 2, 8, 5],
+ [2, 1, 4, 3, 5, 6]),
+ [2, 2, 1, 4, 3, 3, 5, 6, 7, 8, 9],
+ 'example 1';
+ is relative_sort( [3, 3, 4, 6, 2, 4, 2, 1, 3],
+ [1, 3, 2]),
+ [1, 3, 3, 3, 2, 2, 4, 4, 6],
+ 'example 2';
+ is relative_sort( [3, 0, 5, 0, 2, 1, 4, 1, 1],
+ [1, 0, 3, 2]),
+ [1, 1, 1, 0, 0, 3, 2, 4, 5],
+ 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is relative_sort( [-20, 0, 1, 2],
+ [2, 1]),
+ [2, 1, -20, 0],
+ 'negative';
+ }
+
+
+ done_testing;
+ exit;
+}