diff options
| author | boblied <boblied@gmail.com> | 2023-01-13 15:06:58 -0600 |
|---|---|---|
| committer | boblied <boblied@gmail.com> | 2023-01-13 15:06:58 -0600 |
| commit | 060e309523bbbf50e6cb9684d858e7059344f31b (patch) | |
| tree | bbb350799f5bafb564e3e7d795e898454787c2a3 | |
| parent | abda51997e0d1b5cd47eb81da0f83910bd086e55 (diff) | |
| download | perlweeklychallenge-club-060e309523bbbf50e6cb9684d858e7059344f31b.tar.gz perlweeklychallenge-club-060e309523bbbf50e6cb9684d858e7059344f31b.tar.bz2 perlweeklychallenge-club-060e309523bbbf50e6cb9684d858e7059344f31b.zip | |
Week 188
| -rwxr-xr-x[-rw-r--r--] | challenge-188/bob-lied/perl/ch-1.pl | 0 | ||||
| -rwxr-xr-x | challenge-188/bob-lied/perl/ch-2.pl | 71 |
2 files changed, 71 insertions, 0 deletions
diff --git a/challenge-188/bob-lied/perl/ch-1.pl b/challenge-188/bob-lied/perl/ch-1.pl index b67fbc6902..b67fbc6902 100644..100755 --- a/challenge-188/bob-lied/perl/ch-1.pl +++ b/challenge-188/bob-lied/perl/ch-1.pl diff --git a/challenge-188/bob-lied/perl/ch-2.pl b/challenge-188/bob-lied/perl/ch-2.pl new file mode 100755 index 0000000000..e145ecbe23 --- /dev/null +++ b/challenge-188/bob-lied/perl/ch-2.pl @@ -0,0 +1,71 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-2.pl Perl Weekly Challenge Week 188 Task 2 Total Zero +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# You are given two positive integers $x and $y. +# Write a script to find out the number of operations needed to make both ZERO. +# Each operation is made up either of the followings: +# $x = $x - $y if $x >= $y +# or +# $y = $y - $x if $y >= $x (using the original value of $x) +# +# Example 1 Input: $x = 5, $y = 4 Output: 5 +# Example 2 Input: $x = 4, $y = 6 Output: 3 +# Example 3 Input: $x = 2, $y = 5 Output: 4 +# Example 4 Input: $x = 3, $y = 1 Output: 3 +# Example 5 Input: $x = 7, $y = 4 Output: 5 +#============================================================================= + +use v5.36; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub usage { "Usage: $0 x y # x and y positive integers" } + +my ($x, $y) = @ARGV; +die usage() unless $x > 0 && $y > 0; + +say totalZero($x, $y); + +sub totalZero($x, $y) +{ + my $count = 0; + while ( ! ($x == 0 && $y == 0 ) ) + { + my $x0 = $x; + if ( $x >= $y ) + { + $x = $x - $y; + } + if ( $y >= $x && $y >= $x0 ) + { + $y = $y - $x0; + } + $count++; + say "$count: x=$x y=$y" if $Verbose; + } + return $count; +} + +sub runTest +{ + use Test2::V0; + + is(totalZero(0,0), 0, "Example 0"); + is(totalZero(5,4), 5, "Example 1"); + is(totalZero(4,6), 3, "Example 2"); + is(totalZero(2,5), 4, "Example 3"); + is(totalZero(3,1), 3, "Example 4"); + is(totalZero(7,4), 5, "Example 5"); + + done_testing; +} + |
