51
\$\begingroup\$

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 , 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]}
\$\endgroup\$
11
  • \$\begingroup\$ Should we only consider as many groups of 5 as there are underscores in the input? \$\endgroup\$ Commented May 12, 2018 at 18:10
  • \$\begingroup\$ In that case, the rule when there's no _ in the input string is a bit of a special case. \$\endgroup\$ Commented May 12, 2018 at 18:17
  • 1
    \$\begingroup\$ Ooh, Bacon cipher! \$\endgroup\$
    – Deusovi
    Commented May 12, 2018 at 19:38
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – Laikoni
    Commented May 14, 2018 at 11:03
  • 1
    \$\begingroup\$ @KirillL. Yes, anything in [32-126]. I've added another test case. \$\endgroup\$
    – Arnauld
    Commented May 14, 2018 at 12:16

31 Answers 31

1
2
1
\$\begingroup\$

GolfScript, 94 84 82 72 bytes

.{.[65-\97-]{26,?)}/+},5/{{91<}%2base}%:s;0:l;{32|.127={l):l(s=97+}@if}%

Try it online!

This is probably not optimal but I can't find anything shorter.

Explanation:

  • {.[65-\97-]{26,?)}/+},

    Filter by:

    • For each of [inp - 65, inp - 97]:
      • Index of this in [0, 1, ..., 25] (-1 if not found)
      • Add one to get 0 for not found, nonzero otherwise
    • Take the sum of the result, giving 0 iff if both results were 0.
  • 5/ Divide into chunks of length 5.

  • {{91<}%2base}%:s;

    Map each chunk of 5 to:

    • Map each of the 5 to:
      • Is it less than 91? (is it uppercase?)
    • Convert this result from binary to decimal. Assign this to the variable s.
  • 0:l; Assign the variable l to 0.

  • { ... }% Map each character of the input to:

    • 32| Convert to lowercase by bitwise or-ing with 32. This will give 127 for an underscore.

    • .127={ ... }@if Return that unless it is 127 (for us, underscore), in which case...

      • l):l Increment l

      • (s= Index into s

      • 97+ Add 97 to make it a lowercase letter.

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.