diff options
| -rw-r--r-- | challenge-228/bob-lied/README | 6 | ||||
| -rw-r--r-- | challenge-228/bob-lied/perl/ch-1.pl | 58 | ||||
| -rw-r--r-- | challenge-228/bob-lied/perl/ch-2.pl | 67 |
3 files changed, 128 insertions, 3 deletions
diff --git a/challenge-228/bob-lied/README b/challenge-228/bob-lied/README index fb8ad62bfa..6230e78792 100644 --- a/challenge-228/bob-lied/README +++ b/challenge-228/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 226 by Bob Lied +Solutions to weekly challenge 228 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-226/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-226/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-228/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-228/bob-lied diff --git a/challenge-228/bob-lied/perl/ch-1.pl b/challenge-228/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..a6887d202f --- /dev/null +++ b/challenge-228/bob-lied/perl/ch-1.pl @@ -0,0 +1,58 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-1.pl Perl Weekly Challenge Task 1 Unique Sum +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# You are given an array of integers. +# Write a script to find out the sum of unique elements in the given array. +# Example 1 Input: @int = (2, 1, 3, 2) +# Output: 4 +# In the given array we have 2 unique elements (1, 3). +# Example 2 Input: @int = (1, 1, 1, 1) +# Output: 0 +# In the given array no unique element found. +# Example 3 Input: @int = (2, 1, 3, 4) +# Output: 10 +# In the given array every element is unique. +# +#============================================================================= + +use v5.36; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +say uniqueSum(@ARGV); + +sub uniqueSum(@list) +{ + my %count; + + my $sum = 0; + for my $n ( @list ) + { + my $howMany = ++$count{$n}; + + if ( $howMany == 1 ) { $sum += $n } + elsif ( $howMany == 2 ) { $sum -= $n } + # Ignore if present more than twice. + } + return $sum; +} + +sub runTest +{ + use Test2::V0; + + is( uniqueSum(2,1,3,2), 4, "Example 1"); + is( uniqueSum(1,1,1,1), 0, "Example 2"); + is( uniqueSum(2,1,3,4), 10, "Example 3"); + + done_testing; +} diff --git a/challenge-228/bob-lied/perl/ch-2.pl b/challenge-228/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..5b393da670 --- /dev/null +++ b/challenge-228/bob-lied/perl/ch-2.pl @@ -0,0 +1,67 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-2.pl Perl Weekly Challenge 228 Task 2 Empty Array +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# You are given an array of integers in which all elements are unique. +# Write a script to perform the following operations until the array is +# empty and return the total count of operations. If the first element is +# the smallest then remove it otherwise move it to the end. +# Example 1 Input: @int = (3, 4, 2) +# Ouput: 5 +# Operation 1: move 3 to the end: (4, 2, 3) +# Operation 2: move 4 to the end: (2, 3, 4) +# Operation 3: remove element 2: (3, 4) +# Operation 4: remove element 3: (4) +# Operation 5: remove element 4: () + +# Example 2 Input: @int = (1, 2, 3) +# Ouput: 3 +# Operation 1: remove element 1: (2, 3) +# Operation 2: remove element 2: (3) +# Operation 3: remove element 3: () +#============================================================================= + +use v5.36; + +use List::Util qw/min/; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub emptyArray(@int) +{ + my $opCount = 0; + while ( @int ) + { + say "(", join(",", @int), ")" if $Verbose; + $opCount++; + + # Remove first element + my $front = shift(@int); + + # Put it on the end if it wasn't the smallest + push @int, $front if @int && $front > min(@int); + } + return $opCount; +} + +sub runTest +{ + use Test2::V0; + + is( emptyArray(3,4,2), 5, "Example 1"); + is( emptyArray(1,2,3), 3, "Example 2"); + is( emptyArray(5,4,3), 6, "Reverse order"); + is( emptyArray(7 ), 1, "Singleton"); + is( emptyArray( ), 0, "Empty list"); + + + done_testing; +} |
