6 kyu

Reversing a Process

1,117 of 2,897g964

Description:

Suppose we know the process by which a string s was encoded to a string r (see explanation below). The aim of the kata is to decode this string r to get back the original string s.

Explanation of the encoding process:

  • input: a string s composed of lowercase letters from "a" to "z", and a positive integer num
  • we know there is a correspondence between abcde...uvwxyzand 0, 1, 2 ..., 23, 24, 25 : 0 <-> a, 1 <-> b ...
  • if c is a character of s whose corresponding number is x, apply to x the function f: x-> f(x) = num * x % 26, then find ch the corresponding character of f(x)
  • Accumulate all these ch in a string r
  • concatenate num and r and return the result

For example:

encode("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

So we get "ekx", hence the output is "6015ekx"

Task

A string s was encoded to string r by the above process. complete the function to get back s whenever it is possible.

Indeed it can happen that the decoding is impossible for strings composed of whatever letters from "a" to "z" when positive integer num has not been correctly chosen. In that case return "Impossible to decode".

Examples

decode "6015ekx" -> "mer"
decode "5057aan" -> "Impossible to decode"
Fundamentals

More By Author:

Check out these other kata created by g964

Stats:

CreatedOct 21, 2019
PublishedOct 21, 2019
Warriors Trained11881
Total Skips722
Total Code Submissions16588
Total Times Completed2897
Python Completions1117
JavaScript Completions714
Java Completions331
C++ Completions236
Scala Completions34
Kotlin Completions80
Groovy Completions12
C# Completions186
Shell Completions20
Rust Completions60
Haskell Completions39
Ruby Completions57
Haxe Completions5
Dart Completions73
Pascal Completions6
Lua Completions23
Perl Completions11
Raku Completions1
Elm Completions3
D Completions6
Erlang Completions3
Prolog Completions6
PHP Completions12
Total Stars337
% of votes with a positive feedback rating89% of 483
Total "Very Satisfied" Votes401
Total "Somewhat Satisfied" Votes56
Total "Not Satisfied" Votes26
Total Rank Assessments7
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • g964 Avatar
  • jcsahnwaldt Avatar
  • anter69 Avatar
  • imjasonmiller Avatar
  • JohanWiltink Avatar
  • Blind4Basics Avatar
  • monadius Avatar
  • hobovsky Avatar
  • Just4FunCoder Avatar
  • cwps Avatar
Ad