diff options
| author | E. Choroba <choroba@matfyz.cz> | 2020-05-12 00:09:56 +0200 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2020-05-12 00:09:56 +0200 |
| commit | 21e4e088d528c73e1ba8b7d47679518cb8b580cb (patch) | |
| tree | e5253da25b32d3d9c1106e4b95af11d5faa789b6 | |
| parent | 623b1052ee3bdc8f4cabb82d954ef6ca48830830 (diff) | |
| download | perlweeklychallenge-club-21e4e088d528c73e1ba8b7d47679518cb8b580cb.tar.gz perlweeklychallenge-club-21e4e088d528c73e1ba8b7d47679518cb8b580cb.tar.bz2 perlweeklychallenge-club-21e4e088d528c73e1ba8b7d47679518cb8b580cb.zip | |
Add solutions to 060 (Excel Column + Find Numbers) by E. Choroba
| -rwxr-xr-x | challenge-060/e-choroba/perl/ch-1.pl | 48 | ||||
| -rwxr-xr-x | challenge-060/e-choroba/perl/ch-2.pl | 52 |
2 files changed, 100 insertions, 0 deletions
diff --git a/challenge-060/e-choroba/perl/ch-1.pl b/challenge-060/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..c91c6c1a3b --- /dev/null +++ b/challenge-060/e-choroba/perl/ch-1.pl @@ -0,0 +1,48 @@ +#!/usr/bin/perl +use warnings; +use strict; + +sub convert { + my ($in) = @_; + my $r; + if ($in =~ /^[0-9]+$/) { + $r = ""; + while ($in) { + substr $r, 0, 0, chr(--$in % 26 + ord 'A'); + $in = int($in / 26); + } + + } elsif ($in =~ /^[A-Z]+$/) { + $r = 0; + while ($in) { + $r *= 26; + $r += ord(substr $in, 0, 1, "") + 1 - ord 'A'; + } + + } else { + die "Invalid input: $in!\n"; + } + + return $r +} + +use Test::More; + +is convert(1), 'A', 'A'; +is convert(26), 'Z', 'Z'; +is convert(27), 'AA', 'AA'; +is convert(52), 'AZ', 'AZ'; +is convert(53), 'BA', 'BA'; +is convert(789), 'ADI', 'ADI'; + +is convert('A'), 1, 'A'; +is convert('Z'), 26, 'Z'; +is convert('AA'), 27, 'AA'; +is convert('AZ'), 52, 'AZ'; +is convert('BA'), 53, 'BA'; +is convert('ADI'), 789, 'ADI'; + +is convert(28), 'AB', 'encode'; +is convert('AD'), 30, 'decode'; + +done_testing(); diff --git a/challenge-060/e-choroba/perl/ch-2.pl b/challenge-060/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..d469490fd9 --- /dev/null +++ b/challenge-060/e-choroba/perl/ch-2.pl @@ -0,0 +1,52 @@ +#!/usr/bin/perl +use warnings; +use strict; + +sub extend { + my ($length, $short, $long) = @_; + my %next; + undef @next{@$short}; + for my $i (0 .. $#$short) { + for my $j (0 .. $#$short) { + my $new = 0 + ($short->[$i] . $short->[$j]); + next if $length < length $new; + if (length($new) < $length) { + undef $next{$new}; + } else { + undef $long->{$new}; + } + } + } + return keys %$long if @$short == keys %next; + + return extend($length, [keys %next], $long) +} + +sub find { + my ($length, $greater, @list) = @_; + return grep $greater > $_, extend($length, \@list, {}); +} + +use Test::More; +use Test::Deep; + +cmp_deeply [ find(2, 21, 0, 1, 2, 5) ], + bag(10, 11, 12, 15, 20); + +cmp_deeply [ find(4, 3111, 1, 2, 3) ], + bag(glob '{{1,2}{1,2,3}}{{1,2,3}{1,2,3}}'); + +cmp_deeply [ find(5, 20000, 0, 1) ], + bag(glob '1{0,1}{0,1}{0,1}{0,1}'); + +cmp_deeply [ find(3, 222, 2) ], + []; + +cmp_deeply [ find(10, 2000000022, 0, 2) ], + bag(2000000000, 2000000002, 2000000020); + +cmp_deeply [ find(5, 30000, 1, 20, 300) ], + bag(11111, 11120, 11201, 11300, 12011, 12020, + 13001, 20111, 20120, 20201, 20300); + +done_testing(); |
