The Bear's Den

Enter at your own risk

Alternate Codes

Task 1: Decrypt String

Submitted by: Mohammad Sajid Anwar


You are given a string formed by digits and ‘#’.

Write a script to map the given string to English lowercase characters following the given rules.

Example 1

Input: $str = "10#11#12"
Output: "jkab"

10# -> j
11# -> k
1   -> a
2   -> b

Example 2

Input: $str = "1326#"
Output: "acz"

1   -> a
3   -> c
26# -> z

Example 3

Input: $str = "25#24#123"
Output: "yxabc"

25# -> y
24# -> x
1   -> a
2   -> b
3   -> c

Example 4

Input: $str = "20#5"
Output: "te"

20# -> t
5   -> e

Example 5

Input: $str = "1910#26#"
Output: "aijz"

1   -> a
9   -> i
10# -> j
26# -> z

Solution

Search for single digits 0..9 or two digits 10..26 followed by # and use the decremented value as an index into the array 'a'..'z'. The behavior for other characters or substrings is not specified. Passing these unaltered, such that '27#' will be decrypted as 'bg#'.

Perl

Using capture groups to pick the codes.

sub decrypt_string {
    state $alpha = ['a'..'z'];
    shift =~ s/(?|(1[0-9]|2[0-6])#|([1-9]))/$alpha->[$1 - 1]/gr;
}

See the full solution to task 1.

J

Using rxapply to decrypt the numbers. This verb acts on full matches, not capture groups and thus catches the number signs, too. Need to remove these from the matching substrings.

require 'regex'

onIdx =: [.&.:(a:`(].&i.))
alpha =: 26 (+ i.)~ onIdx a. 'a'
coderx =: '(1[0-9]|2[0-6])#|([1-9])'
decrypt =: {&alpha@<:@".@('#'&delstring)
DecryptString =: coderx&(decrypt rxapply)

echo DecryptString '10#11#12'

See the full solution.

Task 2: Goal Parser

Submitted by: Mohammad Sajid Anwar


You are given a string, $str.

Write a script to interpret the given string using Goal Parser.

The Goal Parser interprets “G” as the string “G”, “()” as the string “o”, and “(al)” as the string “al”. The interpreted strings are then concatenated in the original order.

Example 1

Input: $str = "G()(al)"
Output: "Goal"

G    -> "G"
()   -> "o"
(al) -> "al"

Example 2

Input: $str = "G()()()()(al)"
Output: "Gooooal"

G       -> "G"
four () -> "oooo"
(al)    -> "al"

Example 3

Input: $str = "(al)G(al)()()"
Output: "alGaloo"

(al) -> "al"
G    -> "G"
(al) -> "al"
()   -> "o"
()   -> "o"

Example 4

Input: $str = "()G()G"
Output: "oGoG"

() -> "o"
G  -> "G"
() -> "o"
G  -> "G"

Example 5

Input: $str = "(al)(al)G()()"
Output: "alalGoo"

(al) -> "al"
(al) -> "al"
G    -> "G"
()   -> "o"
()   -> "o"

Solution

Again, there is no specification how other strings shall be handled and again passing these unaltered. There is not even the need to handle 'G' specially.

Perl

use strict;
use warnings;

sub goal_parser {
    shift =~ s/(\(al\))|(\(\))/$1 ? 'al' : 'o'/ger;
}

See the full solution to task 2.

J

GoalParser =: ('(al)';'al';'()';'o')&stringreplace

echo GoalParser 'G()(al)'

See the full solution.