This is my response to The Weekly Challenge #358.
@strings.
Input: @strings = ("123", "45", "6")
Output: 123
"123" -> 123
"45" -> 45
"6" -> 6
Example 2:
Input: @strings = ("abc", "de", "fghi")
Output: 4
"abc" -> 3
"de" -> 2
"fghi" -> 4
Example 3:
Input: @strings = ("0012", "99", "a1b2c")
Output: 99
"0012" -> 12
"99" -> 99
"a1b2c" -> 5
Example 4:
Input: @strings = ("x", "10", "xyz", "007")
Output: 10
"x" -> 1
"xyz" -> 3
"007" -> 7
"10" -> 10
Example 5:
Input: @strings = ("hello123", "2026", "perl")
Output: 2026
"hello123" -> 8
"perl" -> 4
"2026" -> 2026
#! /usr/bin/env raku
unit sub MAIN (*@strings where @strings.elems > 0, # [1]
:v(:$verbose));
my $max = -Inf; # [2]
for @strings -> $string # [3]
{
my $value = $string ~~ /^ <[0..9]>+ $/ # [4]
?? $string.Int # [4a]
!! $string.chars; # [4b]
my $is-max = $value > $max; # [5]
$max = $value if $is-max; # [6]
say ": String:'$string' Val:$value \
{ $is-max ?? "new max" !! "" }" if $verbose;
}
say $max; # [7]
[1] A slurpy array of strings, with at least one.
[2] The result will end up here, the maximum value. We start off small.
[3] Iterate over the strings.
[4] Does the string contain digits only? If so, use the integer value [4a]. If not, use the lenght of the string [4b].
[5] Do we have a new maximum?
[6] Update the maxiumum, if we have a new one.
[7] Print the result.
Running it:
$ ./max-string-value 123 45 6
123
$ ./max-string-value abc de fghi
4
$ ./max-string-value 0012 99 a1b2c
99
$ ./max-string-value x 10 xyz 007
10
$ ./max-string-value hello123 2026 perl
2026
Looking good.
With verbose mode:
$ ./max-string-value -v 123 45 6
: String:'123' Val:123 new max
: String:'45' Val:45
: String:'6' Val:6
123
$ ./max-string-value -v abc de fghi
: String:'abc' Val:3 new max
: String:'de' Val:2
: String:'fghi' Val:4 new max
4
$ ./max-string-value -v 0012 99 a1b2c
: String:'0012' Val:12 new max
: String:'99' Val:99 new max
: String:'a1b2c' Val:5
99
$ ./max-string-value -v x 10 xyz 007
: String:'x' Val:1 new max
: String:'10' Val:10 new max
: String:'xyz' Val:3
: String:'007' Val:7
10
$ ./max-string-value -v hello123 2026 perl
: String:'hello123' Val:8 new max
: String:'2026' Val:2026 new max
: String:'perl' Val:4
2026
$str and an integer $int.
Write a script to encrypt the string using the algorithm - for each character
$char in $str, replace $char with the $intth character
after $char in the alphabet, wrapping if needed and return the encrypted
string.
Input: $str = "abc", $int = 1
Output: "bcd"
Example 2:
Input: $str = "xyz", $int = 2
Output: "zab"
Example 3:
Input: $str = "abc", $int = 27
Output: "bcd"
Example 4:
Input: $str = "hello", $int = 5
Output: "mjqqt"
Example 5:
Input: $str = "perl", $int = 26
Output: "perl"
#! /usr/bin/env raku
unit sub MAIN ($str where $str ~~ /^ <[a..z]>+ $/, # [1]
UInt $int); # [2]
say $str.comb>>.&rotate($int).join; # [3]
sub rotate($char, $offset) # [4]
{
my $code = $char.ord + $offset; # [5]
$code -= 26 while $code > 'z'.ord; # [6]
return $code.chr; # [7]
}
[1] A string containing lower case ascii letters only, with at least one.
[2] The number of positions to right shift (or rotate) the letters.
[3] Split the word into individual characters (with comb),
apply my «rotate» procedure on each letter, and join them together
again before printing the resulting string.
[4] The custom procedure doing the rotation (or right shift).
[5] Get the (Unicode) index of the character (with
ord) and add the offset (right shift value).
See docs.raku.org/routine/ord for more information about ord.
[6] Subtract 26 as long as the index is after the «z». This supports offset values as high as you could possible want, as hinted to in the third example.
[7] Convert the index back to a character with
chr, and return it.
See docs.raku.org/routine/chr for more information about chr.
Running it:
$ ./encrypted-string abc 1
bcd
$ ./encrypted-string xyz 2
zab
$ ./encrypted-string abc 27
bcd
$ ./encrypted-string hello 5
mjqqt
$ ./encrypted-string perl 26
perl
Looking good.
Verbose mode did not make much sense for this program.
And that's it.