aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-05-20 20:28:38 +0100
committerGitHub <noreply@github.com>2023-05-20 20:28:38 +0100
commit31dcea4745407688443f81a33b877d54db6bea1a (patch)
tree659521ed0330d461e3d5718f3757d16451f9894b
parent998fb0f3e8bfa868783f58cc6846a91b3e502258 (diff)
parente94e2ffec42fc77da827f8d71d095d50f49b1e13 (diff)
downloadperlweeklychallenge-club-31dcea4745407688443f81a33b877d54db6bea1a.tar.gz
perlweeklychallenge-club-31dcea4745407688443f81a33b877d54db6bea1a.tar.bz2
perlweeklychallenge-club-31dcea4745407688443f81a33b877d54db6bea1a.zip
Merge pull request #8106 from choroba/ech217b
Fix a bug in 217/2
-rwxr-xr-xchallenge-217/e-choroba/perl/ch-2.pl24
1 files changed, 17 insertions, 7 deletions
diff --git a/challenge-217/e-choroba/perl/ch-2.pl b/challenge-217/e-choroba/perl/ch-2.pl
index 209c7ad60d..701a46215d 100755
--- a/challenge-217/e-choroba/perl/ch-2.pl
+++ b/challenge-217/e-choroba/perl/ch-2.pl
@@ -12,17 +12,25 @@ sub max_number(@list) {
}
sub compare($x, $y) {
- return $x <=> $y if length($x) == length($y);
+ my ($lx, $ly) = map length, $x, $y;
+ return $x cmp $y if $lx == $ly;
- my $first = substr $x, 0, 1;
- my $diff = substr $x, length($y), 1;
- my $cmp = $diff <=> $first;
- return $cmp if $cmp || 1 == length $y;
+ my ($posx, $posy) = ($ly, 0);
+ my ($xx, $yy) = ($x, $x);
+ my $over = 0;
+ my $count = 0;
+ while ($over < 2
+ && substr($xx, $posx, 1) eq substr($yy, $posy, 1)
+ ) {
+ ++$count;
+ ++$over, $yy = $x, $posy = 0 if ++$posy >= length $yy;
+ ++$over, $xx = $y, $posx = 0 if ++$posx >= length $xx;
+ }
- return compare(substr($x, 1), substr($y, 1))
+ return substr($xx, $posx, 1) cmp substr($yy, $posy, 1)
}
-use Test::More tests => 5 + 3 * 6 + 3;
+use Test::More tests => 5 + 3 * 6 + 3 + 1;
is max_number(1, 23), 231, 'Example 1';
is max_number(10, 3, 2), 3210, 'Example 2';
@@ -54,3 +62,5 @@ is max_number(44567, 445675), 44567544567, 'recursion > rev';
is max_number(23, 23, 231), 2323231, 'repeated <';
is max_number(23, 23, 232), 2323232, 'repeated =';
is max_number(23, 23, 233), 2332323, 'repeated >';
+
+is max_number(4, 447, 3, 331, 4340, 43), '44744343403331', 'bug';