Suppose we know the process (A) by which a string s
has been coded to a string r
.
The aim of the kata is to decode r
to get back the original string s.
Explanation of the known process (A):
data: a string
s
composed of lowercase letters from a to z and a positive integernum
We know there is a correspondence between
abcde...uvwxyzand
0, 1, 2 ..., 23, 24, 25
:0 <-> a, 1 <-> b ...
If
c
is a character ofs
whose corresponding number isx
, apply to x the functionf: x-> f(x) = num * x % 26
then findch
the corresponding character off(x)
Accumulate all these
ch
in a stringr
.Concatenate
num
andr
and return the result.
Example:
code("mer", 6015) -> "6015ekx"
m <-> 12, 12 * 6015 % 26 == 4, 4 <-> e
e <-> 4, 4 * 6015 % 26 == 10, 10 <-> k
r <-> 17, 17 * 6015 % 26 == 23, 23 <-> x
We get "ekx" hence "6015ekx"
Task:
A string s
has been coded to a string r
by the above process (A). Write a function r -> decode(r) to get back s whenever it is possible.
Indeed it can happen that the decoding is impossible when positive integer num
has not been correctly chosen. In that case return "Impossible to decode".
Example:
decode("6015ekx") -> "mer"
decode("5057aan") -> "Impossible to decode"
This challenge comes from g964 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (3)
TypeScript plus tests
github.com/kiliman/dev-to-daily-ch...
Test
An overengineered arrow-kt solution where I never check explicitly for null.
Probably uglier than needed, but fun.
In python