The Bear's Den

Enter at your own risk

Maximum Encryption

Task 1: Max Str Value

Submitted by: Mohammad Sajid Anwar


You are given an array of alphanumeric string, @strings.

Write a script to find the max value of alphanumeric string in the given array. The numeric representation of the string, if it comprises of digits only otherwise length of the string.

Example 1

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

Solution

Perl

Check if an input string contains digits only and use its value in that case. Otherwise use its length. Find the maximum over all input strings.

use strict;
use warnings;
use List::Util 'max';

sub max_str {
    max map /^\d+$/ ? $_ : length, @_;
}

See the full solution to task 1.

J

At first, we create the string '0123456789' in a funny way:

onIdx =: [.&.:(a:`(].&i.))
digits =: 10 (+ i.)~ onIdx a. '0'

The conjunction onIdx used as x u onIdx n y takes the indices of y within n, applies
x u to them and picks the corresponding elements of n as the result.

Here the index of '0' in the character set is taken (i.e. its ASCII code), the sequence 0 ... 9 is added and the resulting list is transformed into the corresponding characters.

This is a bit longer than the more common

a. {~ 48+i.10

but it includes the code-lookup for the sequence’s starting character.

The usage of this conjunction may be regarded as overkill, but it is useful because it

Acting on digits, the verb MaxStr solves task 1:

MaxStr =: [: >./ #`".@.([: *./ e.&digits)@>
NB.          GGG F EEDD    CCC BBBBBBBBB  A

echo MaxStr '0012'; '99'; 'a1b2c'

A: unbox string
B: test which characters of the string are digits
C: check if all characters are digits
D: select verb from left gerund (E,F) depending on the result of C: E for all digits, F otherwise
E: convert string to number
F: count items
G: take the maximum

See the full solution.

Task 2: Encrypted String

Submitted by: Mohammad Sajid Anwar


You are given a string $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 $int th character after $char in the alphabet, wrapping if needed and return the encrypted string.

Example 1

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"

Solution

Perl

Taking the string 'a...z' and rotate it by $int characters. The original and the rotated string are then used as operands in a tr/// operator. String::Compile::Tr enables this operation on string variables at runtime without evaling the provided operands.

The behavior on characters outside the range from 'a' to 'z' is not specified in the task. This implementation passes such characters unmodified.

use strict;
use warnings;
use String::Compile::Tr;

sub encrypt ($str, $int) {
    state $alpha = join '', 'a' .. 'z';
    $int %= length $alpha;
    my $enc = substr($alpha, $int) . substr($alpha, 0, $int);
    trgen($alpha, $enc, 'r')->($str);
}

See the full solution to task 2.

J

First create the string 'a...z' just like digits in task 1:

onIdx =: [.&.:(a:`(].&i.))        NB. from task 1
alpha =: 26 (+ i.)~ onIdx a. 'a'

Encrypt a string:

encrypt =: (#alpha)&|@:+ onIdx alpha
NB.         BBBBBBBBB  A

echo 2 encrypt 'xyz'

A: add x to the indices of y within alpha
B: take the reminder from the division by the length of alpha

Any characters outside the range from 'a' to 'z' will be treated as 'a'.

See the full solution.