Beta

Alien decipher

Description:

As part of the SETI program, you think you have discovered an alien langage, where each digit from '0' to '9' would be encoded in a sequence of 3 or 4 symbols among _ and |. Of course, there would be one distinct code for each digit.

You indeed received strange messages of two lines each, each line having:

  • 10 digits with each digit from 0 to 9, only once,
  • the corresponding encoded sequence with | and _ (same ordering than the digits above).
Example:  
  4573628901 _||_|___|_|_|____|||_||______||||__
  0754296318 __||_|_||___||_|||________|_||___||

You guess those two lines could be sufficient to decipher the alien code, and you intend to use the same code to reply to aliens, sending your own encoded message...

But you couldn't find a solution by hand, and asked a fellow developer for some help.

You want a function, that takes as parameters (see prototype):

  • the message to encode: a string of some digits from '0' to '9',
  • the clues: an array of two pairs, each pair containing :
    • a string of 10 characters including all the numbers from '0' to '9',
    • a string of characters composed with the symbols _ and | corresponding to the first string, of course in the same order.

The function will return the encoded string corresponding to the message, using the same code that was used for the clues.

Example: with inputs ...

585571121004456
4573628901 _||_|___|_|_|____|||_||______||||__
0754296318 __||_|_||___||_|||________|_||___||

.. your function should return :

|___|||__|___|_|||__||__|||||____||__||_||__||_|_____

... because the only possible code for encoding the first 2 lines is to associate to each digit the following code:

'0' -> "__||"
'1' -> "||__"
'2' -> "|||"
'3' -> "_|_"
'4' -> "_||_"
'5' -> "|__"
'6' -> "___"
'7' -> "_|_|"
'8' -> "_||"
'9' -> "____"

Notes:

  1. All tests (including the random ones) are designed in order to give ONLY one solution. If you have time, figure out the cases when it should not be the case...
  2. All input strings are valid (the purpose of this kata is only to play with algorithms)
  3. Fixed tests: about 50 tests with msg length between 5 and 10 included
  4. Random tests: about 1000 tests with msg length between 10 and 30. Serves as performance test as well if you don't pass rethink your solution...

Enjoy !

Algorithms
Strings

Similar Kata:

More By Author:

Check out these other kata created by eurydice5717

Stats:

CreatedAug 22, 2022
PublishedAug 22, 2022
Warriors Trained72
Total Skips22
Total Code Submissions35
Total Times Completed6
C++ Completions6
Total Stars7
% of votes with a positive feedback rating100% of 4
Total "Very Satisfied" Votes4
Total "Somewhat Satisfied" Votes0
Total "Not Satisfied" Votes0
Total Rank Assessments3
Average Assessed Rank
5 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • eurydice5717 Avatar
Ad