Skip to main content
added 50 characters in body
Source Link
coltim
  • 6.2k
  • 8
  • 13

K (ngn/k), 2524 bytes

-1 byte from @ngn's improvement

{`c$x+`c$x-32*y{y_x,y~:*x}/x}

Try it online!Try it online!

Adapted from my answer on the Speed of Lobsters code golf.

  • y{...}/x set up a reduction seeded with y (the characters to search for), run over the characters in x (the full string). confusingly, within the function itself, x and y are flipped. this ends up returning a boolean array with 1s in the positions containing the desired matches and 0s everywhere else
    • y~:*x compare the first character to search for (*x*x) to the current character being iterated over (yy), updating y with the result of 0 or 1. note that as soon as we have matched all the search characters, no more matches will be identified (since a character will never ~ (match) a 0 or 1)
    • x, append this result to the list of characters to search for (essentially overloading the reduction to end up returning the desired output)
    • y_ if there was a match, drop the first character (if there wasn't a match, this is a no-op). this allows us to search for the next search character in the next iteration of the reduction
  • `c$x+`c$x-32* multiply this bitmask by -3232, representing the offset necessary to capitalize characters at truthy indices, addsubtract it tofrom the original text, and convert it backthat result "back" to a string/text

K (ngn/k), 25 bytes

{`c$x+-32*y{y_x,y~:*x}/x}

Try it online!

Adapted from my answer on the Speed of Lobsters code golf.

  • y{...}/x set up a reduction seeded with y (the characters to search for), run over the characters in x (the full string). confusingly, within the function itself, x and y are flipped. this ends up returning a boolean array with 1s in the positions containing the desired matches and 0s everywhere else
    • y~:*x compare the first character to search for (*x) to the current character being iterated over (y), updating y with the result of 0 or 1. note that as soon as we have matched all the search characters, no more matches will be identified (since a character will never ~ (match) a 0 or 1)
    • x, append this result to the list of characters to search for (essentially overloading the reduction to end up returning the desired output)
    • y_ if there was a match, drop the first character (if there wasn't a match, this is a no-op). this allows us to search for the next search character in the next iteration of the reduction
  • `c$x+-32* multiply this bitmask by -32, representing the offset necessary to capitalize characters at truthy indices, add it to the original text, and convert it back to a string/text

K (ngn/k), 24 bytes

-1 byte from @ngn's improvement

{`c$x-32*y{y_x,y~:*x}/x}

Try it online!

Adapted from my answer on the Speed of Lobsters code golf.

  • y{...}/x set up a reduction seeded with y (the characters to search for), run over the characters in x (the full string). confusingly, within the function itself, x and y are flipped. this ends up returning a boolean array with 1s in the positions containing the desired matches and 0s everywhere else
    • y~:*x compare the first character to search for (*x) to the current character being iterated over (y), updating y with the result of 0 or 1. note that as soon as we have matched all the search characters, no more matches will be identified (since a character will never ~ (match) a 0 or 1)
    • x, append this result to the list of characters to search for (essentially overloading the reduction to end up returning the desired output)
    • y_ if there was a match, drop the first character (if there wasn't a match, this is a no-op). this allows us to search for the next search character in the next iteration of the reduction
  • `c$x-32* multiply this bitmask by 32, representing the offset necessary to capitalize characters at truthy indices, subtract it from the original text, and convert that result "back" to a string/text
Source Link
coltim
  • 6.2k
  • 8
  • 13

K (ngn/k), 25 bytes

{`c$x+-32*y{y_x,y~:*x}/x}

Try it online!

Adapted from my answer on the Speed of Lobsters code golf.

  • y{...}/x set up a reduction seeded with y (the characters to search for), run over the characters in x (the full string). confusingly, within the function itself, x and y are flipped. this ends up returning a boolean array with 1s in the positions containing the desired matches and 0s everywhere else
    • y~:*x compare the first character to search for (*x) to the current character being iterated over (y), updating y with the result of 0 or 1. note that as soon as we have matched all the search characters, no more matches will be identified (since a character will never ~ (match) a 0 or 1)
    • x, append this result to the list of characters to search for (essentially overloading the reduction to end up returning the desired output)
    • y_ if there was a match, drop the first character (if there wasn't a match, this is a no-op). this allows us to search for the next search character in the next iteration of the reduction
  • `c$x+-32* multiply this bitmask by -32, representing the offset necessary to capitalize characters at truthy indices, add it to the original text, and convert it back to a string/text