45
4
Input
A non-empty encoded string consisting of printable ASCII characters (in the range 32-126), where some missing letters have been replaced with _
.
Output
A decoded string of the same length with all letters in lowercase, including the missing ones.
How?
Edit: As mentioned by @Deusovi in the comments, this is a variant of Bacon's cipher.
- Gather all letters in the original string and group them by 5. Additional letters that do not fit in a full group of 5 are ignored.
- Convert each group into binary: lowercase = 0, uppercase = 1. This leads to a list of integers.
- Use each value N in this list to replace each
_
in the original string with the N-th letter of the alphabet (0-indexed), in order of appearance.
Example: prOGraMMIng PuZZleS & cOde ____
prOGr --> 00110 --> 6 --> 7th letter = 'g'
aMMIn --> 01110 --> 14 --> 15th letter = 'o'
gPuZZ --> 01011 --> 11 --> 12th letter = 'l'
leScO --> 00101 --> 5 --> 6th letter = 'f'
By replacing the missing letters and converting everything back to lowercase, the original string is unveiled:
programming puzzles & code golf
This is the expected output.
Clarifications and rules
- The missing letters are guaranteed to appear at the end of the string. More formally: there will never be any letter after the first
_
in the input string. However, there may be other printable ASCII characters such as spaces and punctuation marks. - The input is guaranteed not to contain any useless capital letter: all capital letters are bits set to 1 which are required to decode the missing letters. Everything else is in lowercase.
- The input string is guaranteed to be valid. Especially:
- It will always contain enough full groups of 5 letters to decode the underscores.
- The binary-encoded integers are guaranteed to be in the range [0-25].
- There may be no
_
at all in the input string, in which case you just have to return the input. - This is code-golf, so the shortest answer in bytes wins!
Test cases
Input : hello!
Output: hello!
Input : helLO, worl_!
Output: hello, world!
Input : i aM yoUr faTh__.
Output: i am your father.
Input : prOGraMMIng PuZZleS & cOde ____
Output: programming puzzles & code golf
Input : Can YOu gUesS tHE ENd oF This ____?
Output: can you guess the end of this text?
Input : THe qUICk brown FOx JUMps oVEr the la__ ___.
Output: the quick brown fox jumps over the lazy dog.
Input : RoadS? wHERe we're goinG WE doN't need _____.
Output: roads? where we're going we don't need roads.
Input : thE greatESt Trick thE DeVIl EVer PUllEd wAs CONvInciNg tHe WorLD h_ ____'_ _____.
Output: the greatest trick the devil ever pulled was convincing the world he didn't exist.
Some extra test-cases:
Input : BInar_
Output: binary
Input : 12 MonKey_
Output: 12 monkeys
Input : hyPerbolIZ__
Output: hyperbolized
Input : {[One Last Test ca__]}
Output: {[one last test case]}
Should we only consider as many groups of 5 as there are underscores in the input? – Esolanging Fruit – 2018-05-12T18:10:07.230
In that case, the rule when there's no
_
in the input string is a bit of a special case. – Esolanging Fruit – 2018-05-12T18:17:03.070Sorry, misunderstood your comment (and I think you misunderstood mine). My question is whether we should ignore extra groups of five if there are too few underscores, which seems apparent from the test cases. – Esolanging Fruit – 2018-05-12T18:22:01.880
1Ooh, Bacon cipher! – Deusovi – 2018-05-12T19:38:28.583
@Deusovi Ah, I was pretty sure such a scheme would exist. Thanks for mentioning the right name. Related.
– Arnauld – 2018-05-12T19:52:52.410"There may be no _ at all in the input string, in which case you just have to return the input." - but do we have to lowercase it or not in this case? – SztupY – 2018-05-14T11:00:08.260
1@SztupY As
The input is guaranteed not to contain any useless capital letter
, in case there is no underscore there also won't be a capital letter. – Laikoni – 2018-05-14T11:03:48.970missed that, thanks – SztupY – 2018-05-14T11:22:51.567
Can input contain
[]
brackets (or some other char with ASCII code betweenZ
anda
) in the middle of the string? If so, probably worthy of another test case, as I just tried using[A-z]
regex and only later realized it would fail on such scenario. – Kirill L. – 2018-05-14T12:04:52.4801@KirillL. Yes, anything in
[32-126]
. I've added another test case. – Arnauld – 2018-05-14T12:16:34.340AHK answer is needed – LForchini – 2018-05-16T09:09:08.427