diff options
| author | E. Choroba <choroba@matfyz.cz> | 2024-09-09 09:47:56 +0200 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2024-09-09 09:47:56 +0200 |
| commit | d2b3c44d64c3fa9631fef9f7366ac9521259d2bb (patch) | |
| tree | bc6de13b08650d18eec861d707ce583893a309a0 | |
| parent | 3c67a5382758155040d6598a2fa01ca5fd6d25d9 (diff) | |
| download | perlweeklychallenge-club-d2b3c44d64c3fa9631fef9f7366ac9521259d2bb.tar.gz perlweeklychallenge-club-d2b3c44d64c3fa9631fef9f7366ac9521259d2bb.tar.bz2 perlweeklychallenge-club-d2b3c44d64c3fa9631fef9f7366ac9521259d2bb.zip | |
Add solutions to 286: Self Spammer & Order Game by E. Choroba
| -rwxr-xr-x | challenge-286/e-choroba/perl/ch-1.pl | 11 | ||||
| -rwxr-xr-x | challenge-286/e-choroba/perl/ch-2.pl | 23 |
2 files changed, 34 insertions, 0 deletions
diff --git a/challenge-286/e-choroba/perl/ch-1.pl b/challenge-286/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..5d7490d54c --- /dev/null +++ b/challenge-286/e-choroba/perl/ch-1.pl @@ -0,0 +1,11 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature qw{ say }; + +my @words; +open my $self, '<', __FILE__ or die $!; +while (<$self>) { + push @words, split; +} +say $words[ rand @words ]; diff --git a/challenge-286/e-choroba/perl/ch-2.pl b/challenge-286/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..bf20ab9d36 --- /dev/null +++ b/challenge-286/e-choroba/perl/ch-2.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub order_game(@ints) { + return $ints[0] if 1 == @ints; + + my @mins; + while (my ($x, $y) = splice @ints, 0, 2) { + push @mins, ($x, $y)[($y <=> $x) == @mins % 2 * 2 - 1]; + } + return order_game(@mins) +} + +use Test::More tests => 3 + 2; + +is order_game(2, 1, 4, 5, 6, 3, 0, 2), 1, 'Example 1'; +is order_game(0, 5, 3, 2), 0, 'Example 2'; +is order_game(9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8), 2, 'Example 3'; + +is order_game(42), 42, 'Single number'; +is order_game(3, 7), 3, 'Single tuple'; |
