17
1
Given two strings, find the translation table (substitution cipher) between the two, if the translation is not possible, output false. The answer must be minimized and created from left-to-right. The first character to be translated between words must be the first in the translation table. In addition to this, any letter that is not translated (in the same place as it was originally), should NOT be in the translation table.
Probably most easily defined through examples:
Valid Cases
"bat", "sap" => ["bt","sp"]
Notice the ordering, an output of ["tb","ps"]
is not valid for this challenge.
"sense", "12n12" => ["se","12"]
Notice how the n
isn't translated because it is a 1 to 1 relation.
"rabid", "snail" => ["rabd","snal"]
Notice how the i
isn't translated because it is a 1 to 1 relation.
"ass", "all" => ["s","l"]
A is not included, it stays the same, s
can map to l
due to pattern match.
"3121212", "ABLBLBL" => ["312","ABL"]
Matches pattern perfectly.
Falsy Cases
"banana", "angular" => false
(not the same length, impossible).
"animal", "snails" => false
(each character can only be used ONCE on each side of the translation).
"can","cnn" => false
(n is implicitly used in translation, therefore, defining a translation table with n->a would be invalid)
Thusly, [aimal,sails]
is an invalid answer, making this falsy.
"a1", "22" => false
See "caveats", this is listed as falsy. In this case, it's because a
and 1
cannot both map to 2
. (Each character can only be used ONCE on each side of the translation).
This answer seems to be a good benchmark: https://codegolf.stackexchange.com/a/116807/59376
If you have questions about the functionality of two unlisted word pairs, defer to this implementation.
I/O rules
- Input may be as a 2 element array or as 2 separate inputs.
- Output can be as an array or newline/space delimited, similar to how I have it shown.
- False output may be 0, -1 or false. Erroring/Empty output is also fine.
- You are guaranteed that
a
will not equalb
and neithera
norb
will be empty. a
andb
are printable-ASCII-only sequences of letters.
Caveats
- Translations must occur from left to right, see example 1.
- You must not output characters that remain the same.
- Your program may only take in two strings
a
andb
. - Each character can only be used ONCE on each side of the translation. This is what makes the translation from
snails
toanimals
impossible. - Recursive replaces should not occur. Example of recursive replace:
"a1","22"->[a1,12]
where a is first replaced by a 1, then both resultant 1's are replaced with 2's. This is not correct, assume all translations occur independent of each other, meaning this is falsy. Meaning: "a1" with translation table of [a1,12] evaluates to 12 (not 22)
Labeling this "translation" as a simple substitution cipher might help clarify the intent. – Greg Martin – 2017-04-17T16:47:13.473
Are associative arrays allowed as output? It could save me some bytes – Jörg Hülsermann – 2017-04-19T17:47:24.257
@JörgHülserman I'm not fully sure of the implications of allowing this, maybe make 2 versions so I can see the difference? I'll edit it if I think it isn't harmful to the challenge. – Magic Octopus Urn – 2017-04-19T17:52:57.993
Look at my post first solution as string and the second has as output an associative array – Jörg Hülsermann – 2017-04-19T18:01:26.047
@JörgHülsermann ahhh... I see how you're using it now, I think I'm going to disallow that, not all languages support hash-like structures. – Magic Octopus Urn – 2017-04-19T18:03:07.200
Is an underscore allowed as separator? It would save me 2 Bytes – Jörg Hülsermann – 2017-04-19T23:37:37.373
not all languages support hash-like structures Not all languages natively support matrices (PHP for example does not, while most Eso-Langs do); but I haven´t seen them disallowed anywhere. – Titus – 2017-04-20T13:01:27.207
@Titus JavaScript's basic arrays are also technically hash-like structures as well – fəˈnɛtɪk – 2017-04-20T13:26:19.373