diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-14 15:30:09 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-14 15:30:09 +0100 |
| commit | 8831c5149d8584bb38fd6c8df46e884d26e99abe (patch) | |
| tree | c1edb0de3fed52b9f67cc0e84d7b11e964827fa6 | |
| parent | 555f8b5bdaa7d3ada2a46316c7a70b76a2c590d2 (diff) | |
| download | perlweeklychallenge-club-8831c5149d8584bb38fd6c8df46e884d26e99abe.tar.gz perlweeklychallenge-club-8831c5149d8584bb38fd6c8df46e884d26e99abe.tar.bz2 perlweeklychallenge-club-8831c5149d8584bb38fd6c8df46e884d26e99abe.zip | |
Add Perl solution to challenge 060
| -rw-r--r-- | challenge-060/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-060/paulo-custodio/README | 1 | ||||
| -rw-r--r-- | challenge-060/paulo-custodio/perl/ch-1.pl | 53 | ||||
| -rw-r--r-- | challenge-060/paulo-custodio/perl/ch-2.pl | 49 | ||||
| -rw-r--r-- | challenge-060/paulo-custodio/t/test-1.yaml | 30 | ||||
| -rw-r--r-- | challenge-060/paulo-custodio/t/test-2.yaml | 5 |
6 files changed, 140 insertions, 0 deletions
diff --git a/challenge-060/paulo-custodio/Makefile b/challenge-060/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-060/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-060/paulo-custodio/README b/challenge-060/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-060/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-060/paulo-custodio/perl/ch-1.pl b/challenge-060/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..6aac747b1a --- /dev/null +++ b/challenge-060/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,53 @@ +#!/usr/bin/env perl + +# Challenge 060 +# +# TASK #1 › Excel Column +# Reviewed by: Ryan Thompson +# 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 + +use Modern::Perl; + +my $arg = shift||""; +if ($arg =~ /^\d+$/) { + say num2col($arg); +} +elsif ($arg =~ /^[A-Z]+$/) { + say col2num($arg); +} +else { + die "invalid column: $arg\n"; +} + +sub col2num { + my($col) = @_; + my $num = 0; + for my $digit (split //, uc($col)) { + $num = $num*26 + ord($digit) - ord('A') + 1; + } + return $num; +} + +sub num2col { + my($num) = @_; + my $col = ""; + while ($num > 0) { + my $digit = ($num-1) % 26; + $num = int(($num-1) / 26); + $col = chr(ord('A') + $digit).$col; + } + return $col; +} diff --git a/challenge-060/paulo-custodio/perl/ch-2.pl b/challenge-060/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..6ec49647e7 --- /dev/null +++ b/challenge-060/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,49 @@ +#!/usr/bin/env perl + +# Challenge 060 +# +# TASK #2 › Find Numbers +# Reviewed by: Ryan Thompson +# 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 + +use Modern::Perl; + +my($X, $Y, @L) = @ARGV; +say join(", ", grep {length($_)==$X && $_<$Y} combine(@L)); + +my %combin; + +sub combine1 { + my($prefix, $n, @digits) = @_; + if (length($prefix) == $n) { + $combin{0+$prefix} = 1; + } + else { + for my $digit (@digits) { + combine1($prefix.$digit, $n, @digits); + } + } +} + +sub combine { + my(@digits) = @_; + %combin = (); + for my $n (1 .. @digits) { + combine1("", $n, @digits); + } + return sort {$a <=> $b} keys %combin; +} diff --git a/challenge-060/paulo-custodio/t/test-1.yaml b/challenge-060/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..c40a06c239 --- /dev/null +++ b/challenge-060/paulo-custodio/t/test-1.yaml @@ -0,0 +1,30 @@ +- setup: + cleanup: + args: 1 + input: + output: A +- setup: + cleanup: + args: 30 + input: + output: AD +- setup: + cleanup: + args: A + input: + output: 1 +- setup: + cleanup: + args: AD + input: + output: 30 +- setup: + cleanup: + args: HELLO + input: + output: 3752127 +- setup: + cleanup: + args: 3752127 + input: + output: HELLO diff --git a/challenge-060/paulo-custodio/t/test-2.yaml b/challenge-060/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..58364f5e28 --- /dev/null +++ b/challenge-060/paulo-custodio/t/test-2.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 2 21 0 1 2 5 + input: + output: 10, 11, 12, 15, 20 |
