diff options
| -rw-r--r-- | challenge-268/bob-lied/README | 6 | ||||
| -rw-r--r-- | challenge-268/bob-lied/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-268/bob-lied/perl/ch-1.pl | 49 | ||||
| -rw-r--r-- | challenge-268/bob-lied/perl/ch-2.pl | 48 |
4 files changed, 101 insertions, 3 deletions
diff --git a/challenge-268/bob-lied/README b/challenge-268/bob-lied/README index 164da38120..8e57c28d35 100644 --- a/challenge-268/bob-lied/README +++ b/challenge-268/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 267 by Bob Lied +Solutions to weekly challenge 268 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-267/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-267/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-268/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-268/bob-lied diff --git a/challenge-268/bob-lied/blog.txt b/challenge-268/bob-lied/blog.txt new file mode 100644 index 0000000000..79afceeb68 --- /dev/null +++ b/challenge-268/bob-lied/blog.txt @@ -0,0 +1 @@ +https://dev.to/boblied/pwc-268-games-numbers-play-3c6h-temp-slug-4053796 diff --git a/challenge-268/bob-lied/perl/ch-1.pl b/challenge-268/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..e4512bc29d --- /dev/null +++ b/challenge-268/bob-lied/perl/ch-1.pl @@ -0,0 +1,49 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# ch-1.pl Perl Weekly Challenge 268 Task 1 Magic Number +#============================================================================= +# You are given two arrays of integers of same size, @x and @y. +# Write a script to find the magic number that when added to each elements +# of one of the array gives the second array. Elements order is not important. +# Example 1 Input: @x = (3, 7, 5) @y = (9, 5, 7) +# Output: 2 +# The magic number is 2. +# @x = (3, 7, 5) +# + 2 2 2 +# @y = (5, 9, 7) +# Example 2 Input: @x = (1, 2, 1) @y = (5, 4, 4) +# Output: 3 +# Example 3 Input: @x = (2) @y = (5) +# Output: 3 +#============================================================================= + +use v5.38; + +use builtin qw/true false/; no warnings "experimental::builtin"; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub magicNumber($x, $y) +{ + use List::Util qw/min/; + return min($y->@*) - min($x->@*); +} + +sub runTest +{ + use Test2::V0; + + is( magicNumber([3,7,5], [9,5,7]), 2, "Example 1"); + is( magicNumber([1,2,1], [5,4,4]), 3, "Example 2"); + is( magicNumber([2 ], [5 ]), 3, "Example 3"); + + done_testing; +} diff --git a/challenge-268/bob-lied/perl/ch-2.pl b/challenge-268/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..a9c2b1c4a4 --- /dev/null +++ b/challenge-268/bob-lied/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# +# ch-2.pl Perl Weekly Challenge 268 Task 2 Number Game +#============================================================================= +# You are given an array of integers, @ints, with even number of elements. +# Write a script to create a new array made up of elements of the given +# array. Pick the two smallest integers and add it to new array in decreasing +# order i.e. high to low. Keep doing until the given array is empty. +# Example 1 Input: @ints = (2, 5, 3, 4) +# Output: (3, 2, 5, 4) +# Round 1: we picked (2, 3) and push it to the new array (3, 2) +# Round 2: we picked the remaining (4, 5) and push it to the new array (5, 4) +# Example 2 Input: @ints = (9, 4, 1, 3, 6, 4, 6, 1) +# Output: (1, 1, 4, 3, 6, 4, 9, 6) +# Example 3 Input: @ints = (1, 2, 2, 3) +# Output: (2, 1, 3, 2) +#============================================================================= + +use v5.38; + +use builtin qw/true false/; no warnings "experimental::builtin"; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub game(@ints) +{ + return [ (sort { $a <=> $b } @ints)[ map { 2*$_+1, 2*$_ } 0 .. $#ints/2 ] ]; +} + +sub runTest +{ + use Test2::V0; + + is( game(2,5,3,4), [3,2,5,4], "Example 1"); + is( game(9,4,1,3,6,4,6,1), [1,1,4,3,6,4,9,6], "Example 2"); + is( game(1,2,2,3), [2,1,3,2], "Example 3"); + + done_testing; +} |
