diff options
| author | E. Choroba <choroba@matfyz.cz> | 2023-05-20 17:11:28 +0200 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2023-05-20 17:11:28 +0200 |
| commit | e94e2ffec42fc77da827f8d71d095d50f49b1e13 (patch) | |
| tree | a4bc0a0067864cb584c2787067f7d2515a5bcb58 | |
| parent | 22cd1b407333e4a2849bff1c339f206281bfd990 (diff) | |
| download | perlweeklychallenge-club-e94e2ffec42fc77da827f8d71d095d50f49b1e13.tar.gz perlweeklychallenge-club-e94e2ffec42fc77da827f8d71d095d50f49b1e13.tar.bz2 perlweeklychallenge-club-e94e2ffec42fc77da827f8d71d095d50f49b1e13.zip | |
Fix a bug in 217/2
| -rwxr-xr-x | challenge-217/e-choroba/perl/ch-2.pl | 24 |
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'; |
