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]}
_
in the input string is a bit of a special case. \$\endgroup\$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. \$\endgroup\$[32-126]
. I've added another test case. \$\endgroup\$