aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-05-11 17:34:10 +0100
committerGitHub <noreply@github.com>2020-05-11 17:34:10 +0100
commitd59c3511780b3882e24d3a45d5184e01ab7f4890 (patch)
treecc7d7bbb25a135592bf6703f7fdc0021bdad67dc
parent2e5850c947506818457a2936d8e9e3a04c8cc1cc (diff)
parent66e41d2ebc60e26aeeb34afd88f527a3d564e4f6 (diff)
downloadperlweeklychallenge-club-d59c3511780b3882e24d3a45d5184e01ab7f4890.tar.gz
perlweeklychallenge-club-d59c3511780b3882e24d3a45d5184e01ab7f4890.tar.bz2
perlweeklychallenge-club-d59c3511780b3882e24d3a45d5184e01ab7f4890.zip
Merge pull request #1702 from wanderdoc/master
Solutions to challenge-060.
-rw-r--r--challenge-060/wanderdoc/perl/ch-1.pl64
-rw-r--r--challenge-060/wanderdoc/perl/ch-2.pl55
2 files changed, 119 insertions, 0 deletions
diff --git a/challenge-060/wanderdoc/perl/ch-1.pl b/challenge-060/wanderdoc/perl/ch-1.pl
new file mode 100644
index 0000000000..339f50012b
--- /dev/null
+++ b/challenge-060/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,64 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+Write a script that accepts a number and returns the Excel Column Name it represents and vice-versa.
+Excel columns start at A and increase lexicographically using the 26 letters of the English alphabet, A..Z. After Z, the columns pick up an extra "digit", going from AA, AB, etc., which could (in theory) continue to an arbitrary number of digits. In practice, Excel sheets are limited to 16,384 columns.
+Example:
+Input Number: 28 Output: AB
+Input Column Name: AD Output: 30## Task 2: Bit Sum
+=cut
+
+
+my $re_num = qr/^[0-9]+$/;
+my $re_alp = qr/^[A-Z]+$/i;
+
+my $input = shift or die "Usage: <script> <column number> *OR* <column name>!\n";
+
+my $num_or_alp = $input =~ $re_num ? 1 :
+ $input =~ $re_alp ? 2 : 99;
+die "Input ${input} is wrong!$/" if 99 == $num_or_alp;
+
+$input = uc $input;
+
+
+
+my $LIMIT = 16_384;
+my @array;
+my $x = 1;
+
+while ( @array < $LIMIT )
+{
+ my @symbols = 'A' x $x .. 'Z' x $x;
+
+ push @array, @symbols;
+ $x++;
+}
+@array = @array[0 .. $LIMIT];
+
+
+unshift @array, 'NULL';
+
+my %to_excel;
+@to_excel{keys @array} = @array;
+
+@array = ();
+my %from_excel = reverse %to_excel;
+
+
+if ( 1 == $num_or_alp )
+{
+ print exists $to_excel{$input} ?
+ "The name of the $input column is $to_excel{$input}.$/" :
+ "No such column in Excel!$/";
+}
+
+
+
+if ( 2 == $num_or_alp )
+{
+ print exists $from_excel{$input} ?
+ "The number of the $input column is $from_excel{$input}.$/" :
+ "No such column in Excel!$/";
+} \ No newline at end of file
diff --git a/challenge-060/wanderdoc/perl/ch-2.pl b/challenge-060/wanderdoc/perl/ch-2.pl
new file mode 100644
index 0000000000..680c4484a5
--- /dev/null
+++ b/challenge-060/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,55 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+Write a script that accepts list of positive numbers (@L) and two positive numbers $X and $Y.
+The script should print all possible numbers made by concatenating the numbers from @L, whose length is exactly $X but value is less than $Y.
+Example Input:
+@L = (0, 1, 2, 5);
+$X = 2;
+$Y = 21;
+Output: 10, 11, 12, 15, 20
+
+=cut
+
+
+use Getopt::Long;
+use List::Util qw(first);
+my @numbers;
+my $X = 2;
+my $Y = 21;
+
+GetOptions ("integers|i:i{,}" => \@numbers,
+ "length|l=i" => \$X,
+ "value|v=i" => \$Y)
+
+or die("Error in command line arguments.\n");
+# It seems that GetOptions does not die if there are letters among numbers,
+# the letters and everything after a letter are simply ignored.
+# A '0' is in the array instead then.
+die "Something wrong with the list of positive integers!$/" if
+( scalar @numbers < 2 or
+ defined first{$_ < 0} @numbers
+
+); # Integers > 9 are possible but may not be splitted.
+my $string = join(',',@numbers);
+
+my %seen; # To exclude repetitions because of duplicates in the integer list.
+
+
+for my $i ( 1 .. $X ) # To allow sets with integers > 9.
+{
+ while ( my $comb = glob "{$string}" x $i )
+ {
+ $comb *= 1; # Leading zeros.
+
+ next unless length($comb) == $X;
+
+
+ if ( $comb < $Y )
+ {
+ print $comb, $/ unless $seen{$comb}++;
+ }
+ }
+} \ No newline at end of file