aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-05-12 03:03:19 +0100
committerGitHub <noreply@github.com>2020-05-12 03:03:19 +0100
commitc39f889b0c52662b314930a2e9bb7a4694a9c121 (patch)
treed75832956ff9efb3d59e1cff8d079179996c18af
parent9df11513a4ae1ae5e7bb5a03db5a74ee82573331 (diff)
parent21e4e088d528c73e1ba8b7d47679518cb8b580cb (diff)
downloadperlweeklychallenge-club-c39f889b0c52662b314930a2e9bb7a4694a9c121.tar.gz
perlweeklychallenge-club-c39f889b0c52662b314930a2e9bb7a4694a9c121.tar.bz2
perlweeklychallenge-club-c39f889b0c52662b314930a2e9bb7a4694a9c121.zip
Merge pull request #1704 from choroba/ech060
Add solutions to 060 (Excel Column + Find Numbers) by E. Choroba
-rwxr-xr-xchallenge-060/e-choroba/perl/ch-1.pl48
-rwxr-xr-xchallenge-060/e-choroba/perl/ch-2.pl52
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();