diff options
| author | Bob Lied <boblied+github@gmail.com> | 2024-03-20 12:21:37 -0500 |
|---|---|---|
| committer | Bob Lied <boblied+github@gmail.com> | 2024-03-20 12:21:37 -0500 |
| commit | cf83eac3096b17c8ffb1cc6e40419ab159ec0d1f (patch) | |
| tree | 48ffe6be2bc1a737f6f762567034352102aff3d6 | |
| parent | 5395f01802fbf21afed5c5e25a44e46361431be1 (diff) | |
| download | perlweeklychallenge-club-cf83eac3096b17c8ffb1cc6e40419ab159ec0d1f.tar.gz perlweeklychallenge-club-cf83eac3096b17c8ffb1cc6e40419ab159ec0d1f.tar.bz2 perlweeklychallenge-club-cf83eac3096b17c8ffb1cc6e40419ab159ec0d1f.zip | |
Week 261 solutions
| -rw-r--r-- | challenge-261/bob-lied/README | 6 | ||||
| -rw-r--r-- | challenge-261/bob-lied/perl/ch-1.pl | 56 | ||||
| -rw-r--r-- | challenge-261/bob-lied/perl/ch-2.pl | 58 |
3 files changed, 117 insertions, 3 deletions
diff --git a/challenge-261/bob-lied/README b/challenge-261/bob-lied/README index 4ba7ab0b4f..adeddf3e81 100644 --- a/challenge-261/bob-lied/README +++ b/challenge-261/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 260 by Bob Lied +Solutions to weekly challenge 261 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-260/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-260/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-261/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-261/bob-lied diff --git a/challenge-261/bob-lied/perl/ch-1.pl b/challenge-261/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..6aff905cb8 --- /dev/null +++ b/challenge-261/bob-lied/perl/ch-1.pl @@ -0,0 +1,56 @@ +#!/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 261 Task 1 Element Digit Sum +#============================================================================= +# You are given an array of integers, @ints. +# Write a script to evaluate the absolute difference between element +# and digit sum of the given array. +# Example 1 Input: @ints = (1,2,3,45) Output: 36 +# Element Sum: 1 + 2 + 3 + 45 = 51 +# Digit Sum: 1 + 2 + 3 + 4 + 5 = 15 +# Absolute Difference: | 51 - 15 | = 36 +# Example 2 Input: @ints = (1,12,3) Output: 9 +# Element Sum: 1 + 12 + 3 = 16 +# Digit Sum: 1 + 1 + 2 + 3 = 7 +# Absolute Difference: | 16 - 7 | = 9 +# Example 3 Input: @ints = (1,2,3,4) Output: 0 +# Element Sum: 1 + 2 + 3 + 4 = 10 +# Digit Sum: 1 + 2 + 3 + 4 = 10 +# Absolute Difference: | 10 - 10 | = 0 +# Example 4 Input: @ints = (236, 416, 336, 350) Output: 1296 +#============================================================================= + +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; + +say elementDigitSum(@ARGV); + +sub elementDigitSum(@ints) +{ + use List::Util qw/sum0/; + return abs(sum0(@ints) - sum0( split('', join('', @ints)))); +} + +sub runTest +{ + use Test2::V0; + + is( elementDigitSum(1,2,3,45 ), 36, "Example 1"); + is( elementDigitSum(1,12,3 ), 9, "Example 2"); + is( elementDigitSum(1,2,3,4 ), 0, "Example 3"); + is( elementDigitSum(236,416,336,350), 1296, "Example 4"); + is( elementDigitSum( ), 0, "Empty list"); + + done_testing; +} diff --git a/challenge-261/bob-lied/perl/ch-2.pl b/challenge-261/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..51379cdd76 --- /dev/null +++ b/challenge-261/bob-lied/perl/ch-2.pl @@ -0,0 +1,58 @@ +#!/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 261 Task 2 Multiply By Two +#============================================================================= +# You are given an array of integers, @ints and an integer $start. +# Write a script to do the followings: +# a) Look for $start in the array @ints, if found multiply the number by 2 +# b) If not found stop the process otherwise repeat +# In the end return the final value. +# Example 1 Input: @ints = (5,3,6,1,12) and $start = 3 +# Output: 24 +# Step 1: 3 is in the array so 3 x 2 = 6 +# Step 2: 6 is in the array so 6 x 2 = 12 +# Step 3: 12 is in the array so 12 x 2 = 24 +# 24 is not found in the array so return 24. +# Example 2 Input: @ints = (1,2,4,3) and $start = 1 +# Output: 8 +# Step 1: 1 is in the array so 1 x 2 = 2 +# Step 2: 2 is in the array so 2 x 2 = 4 +# Step 3: 4 is in the array so 4 x 2 = 8 +# 8 is not found in the array so return 8. +# Example 3 Input: @ints = (5,6,7) and $start = 2 +# Output: 2 +# 2 is not found in the array so return 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 multiplyByTwo($start, @ints) +{ + my %present = map { $_ => true } @ints; + $start *= 2 while ( exists $present{$start} ); + return $start; +} + +sub runTest +{ + use Test2::V0; + + is( multiplyByTwo(3, (5,3,6,1,12) ), 24, "Example 1"); + is( multiplyByTwo(1, (1,2,4,3) ), 8, "Example 2"); + is( multiplyByTwo(2, (5,6,7) ), 2, "Example 3"); + + done_testing; +} |
