diff options
| author | Luca Ferrari <fluca1978@gmail.com> | 2020-05-11 08:27:44 +0200 |
|---|---|---|
| committer | Luca Ferrari <fluca1978@gmail.com> | 2020-05-11 08:27:44 +0200 |
| commit | 72befa7c5d2de9e9a9029baccb6d49d81b5dd9db (patch) | |
| tree | b14544ee62fb6357694f02232e6afb2227d1d1cb | |
| parent | 522c90778278b51cb423ef154c1f6ee057077c09 (diff) | |
| download | perlweeklychallenge-club-72befa7c5d2de9e9a9029baccb6d49d81b5dd9db.tar.gz perlweeklychallenge-club-72befa7c5d2de9e9a9029baccb6d49d81b5dd9db.tar.bz2 perlweeklychallenge-club-72befa7c5d2de9e9a9029baccb6d49d81b5dd9db.zip | |
Task 1 done.
| -rw-r--r-- | challenge-060/luca-ferrari/raku/ch-1.p6 | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/challenge-060/luca-ferrari/raku/ch-1.p6 b/challenge-060/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..f052ce5bb4 --- /dev/null +++ b/challenge-060/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,52 @@ +#!env raku + +# 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 + + + +sub MAIN( $what ) { + my @letters = 'A' .. 'Z'; + my @column-name; + + + # numeric value to be converted into a letter + if $what ~~ Int && $what > 0 && $what <= 16_384 { + my $column = $what.Int; + + + while ( $column > @letters.elems ) { + @column-name.push: @letters[ $column / @letters.elems - 1 ]; + $column = $column % @letters.elems; + } + + @column-name.push: @letters[ $column - 1 ]; + say "Cell $what is { @column-name.join }"; + } + elsif $what ~~ Str { + my $column; + my $multiplier = 0; + # string, try to find the cell number + for $what.comb.reverse -> $current_letter { + $column += @letters.first( $current_letter, :k ) + 1 + ( @letters.elems - 1 ) * $multiplier++; + } + + say "Cell $what is $column"; + } + + + + + +} |
