Skip to main content
Source Link
ais523
  • 11
  • 20
  • 35

Brachylog (v2), 10 bytes

↔{|ụ}ᵐ↔w₆⊇

Try it online!

Function submission, using both its arguments (input string on the left, uppercase acronym on the right) and outputting to standard output. Very inefficient (O(2n) performance).

Explanation

↔{|ụ}ᵐ↔w₆⊇
     ᵐ      Iterate over {the left argument}
↔     ↔       in reverse order
 {|           either doing nothing (try this option first)
  |ụ}         or uppercasing {the character being iterated over}
         ⊇  such that {the right argument} is a subsequence of {the result}
       w₆   and when we find it, output {the result of the iteration}

Uppercase some of the characters so that we can find the acronym in the uppercased string. The inefficiency comes from the fact that the iteration order will try uppercasing the first character, then the second, then the first two, then the third, then the first and third, then the second and third, then the first three, then the fourth, etc. – the interpreter doesn't realise that the first solution it finds will uppercase a number of characters exactly equal to the length of the right-hand argument (because any solution which uppercases too many characters will be found later, and any solution that doesn't uppercase enough won't match), so it just tries all possibilities within a given prefix before going onto the next character.

Iterating in reverse is required for the uppercasing of prefixes to be the first thing the interpreter requires (with the normal iteration method, it would try uppercasing the last character first, etc.).

w₆ (which outputs the current value, without discarding it, and where the output is used only if all the constraints in the rest of the program end up matching) is by far the most useful of Brachylog's nine write instructions, so could probably do with a 1-byte representation (I would likely make this the default behaviour for a write if I were designing a Brachylog-alike from scratch).

Post Made Community Wiki by ais523