aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTorgny Lyon <torgny@abc.se>2024-09-09 18:30:59 +0200
committerTorgny Lyon <torgny@abc.se>2024-09-09 19:41:09 +0200
commiteb3dc2e59549db57fa49ce97688355010ac9a584 (patch)
treec7949d69fc5aa34107fdd93c56721be68865e2bc
parent148ad068f27ef5bbdcac38b14685b2273b0d02ec (diff)
downloadperlweeklychallenge-club-eb3dc2e59549db57fa49ce97688355010ac9a584.tar.gz
perlweeklychallenge-club-eb3dc2e59549db57fa49ce97688355010ac9a584.tar.bz2
perlweeklychallenge-club-eb3dc2e59549db57fa49ce97688355010ac9a584.zip
Add solutions for week 286
-rw-r--r--challenge-286/torgny-lyon/blog.txt1
-rwxr-xr-xchallenge-286/torgny-lyon/perl/ch-1.pl8
-rwxr-xr-xchallenge-286/torgny-lyon/perl/ch-2.pl22
3 files changed, 31 insertions, 0 deletions
diff --git a/challenge-286/torgny-lyon/blog.txt b/challenge-286/torgny-lyon/blog.txt
new file mode 100644
index 0000000000..dc04de6771
--- /dev/null
+++ b/challenge-286/torgny-lyon/blog.txt
@@ -0,0 +1 @@
+https://www.abc.se/~torgny/pwc.html#286
diff --git a/challenge-286/torgny-lyon/perl/ch-1.pl b/challenge-286/torgny-lyon/perl/ch-1.pl
new file mode 100755
index 0000000000..63bc588059
--- /dev/null
+++ b/challenge-286/torgny-lyon/perl/ch-1.pl
@@ -0,0 +1,8 @@
+#!/usr/bin/perl
+
+use v5.40;
+
+open my $f, '<', $0;
+undef $/;
+@_ = split /\s+/, <$f>;
+say $_[ int rand @_ ];
diff --git a/challenge-286/torgny-lyon/perl/ch-2.pl b/challenge-286/torgny-lyon/perl/ch-2.pl
new file mode 100755
index 0000000000..0044409085
--- /dev/null
+++ b/challenge-286/torgny-lyon/perl/ch-2.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/perl
+
+use v5.40;
+
+use Test::More tests => 3;
+
+use List::Util qw(min max pairs);
+
+sub play_game {
+ return min(@_) if @_ == 2;
+ my $f = \&max;
+ return play_game(
+ map {
+ $f = $f == \&min ? \&max : \&min;
+ $f->($_->[0], $_->[1]);
+ } pairs @_
+ );
+}
+
+is(play_game(2, 1, 4, 5, 6, 3, 0, 2), 1);
+is(play_game(0, 5, 3, 2), 0);
+is(play_game(9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8), 2);