Skip to main content
deleted 1293 characters in body
Source Link
Dominic van Essen
  • 37.2k
  • 2
  • 24
  • 61

R, 50 bytes

function(s,r=utf8ToInt(s))intToUtf8(16*r%%16+r/16) 

Try it online!

Much more boring than my previous approach, but 7 bytes shorter.


Previous approach:

R, 57 bytes

function(s)intToUtf8(t(matrix(0:255,16))[utf8ToInt(s)+1]) 

Try it online!

There are 2 common-in-code-golf tasks that are awfully painful in R, and these are base-conversion and manipulating strings. The OP (pajonk) knows this, and is probably smirking right now.

The most straightforward way to perform base-conversion in R would be using integer-division, modulo, and exponentiation, so we could flip 2 hex digits of n like this:
n%/%16^(0:1)%%16%*%16^(1:0).
Unfortunately, though, this doesn't vectorize, so we'd need a costly sapply and additional function definition to run it for each character of the input string in turn.

However, since in this case we know that there are exactly 16x16 possibilities, we can just build a look-up table and use vectorized indexing to retrieve the element with flipped hex digits:
matrix(0:255,16) = 16x16 matrix of the numbers 0:255
t() = transpose it, so the number at each position is the position with its hex digits flipped.
[] = element at that position.

So all we need to do now is get the Utf8 character codes from the input, and convert the looked-up values back to Utf8 characters, frustratingly needing both of the horribly-verbose R functions utf8ToInt() and intToUtf8(). Even with the long function names, this turns-out to be a couple of bytes shorter than only using one of them by building a look-up table directly of characters to match (try it here), which would also require vectors of characters as input instead of true 'strings'.

R, 57 bytes

function(s)intToUtf8(t(matrix(0:255,16))[utf8ToInt(s)+1]) 

Try it online!

There are 2 common-in-code-golf tasks that are awfully painful in R, and these are base-conversion and manipulating strings. The OP (pajonk) knows this, and is probably smirking right now.

The most straightforward way to perform base-conversion in R would be using integer-division, modulo, and exponentiation, so we could flip 2 hex digits of n like this:
n%/%16^(0:1)%%16%*%16^(1:0).
Unfortunately, though, this doesn't vectorize, so we'd need a costly sapply and additional function definition to run it for each character of the input string in turn.

However, since in this case we know that there are exactly 16x16 possibilities, we can just build a look-up table and use vectorized indexing to retrieve the element with flipped hex digits:
matrix(0:255,16) = 16x16 matrix of the numbers 0:255
t() = transpose it, so the number at each position is the position with its hex digits flipped.
[] = element at that position.

So all we need to do now is get the Utf8 character codes from the input, and convert the looked-up values back to Utf8 characters, frustratingly needing both of the horribly-verbose R functions utf8ToInt() and intToUtf8(). Even with the long function names, this turns-out to be a couple of bytes shorter than only using one of them by building a look-up table directly of characters to match (try it here), which would also require vectors of characters as input instead of true 'strings'.

R, 50 bytes

function(s,r=utf8ToInt(s))intToUtf8(16*r%%16+r/16) 

Try it online!

Much more boring than my previous approach, but 7 bytes shorter.


Previous approach:

R, 57 bytes

function(s)intToUtf8(t(matrix(0:255,16))[utf8ToInt(s)+1]) 

Try it online!

added 429 characters in body
Source Link
Dominic van Essen
  • 37.2k
  • 2
  • 24
  • 61

R, 57 bytes

function(s)intToUtf8(t(matrix(0:255,16))[utf8ToInt(s)+1]) 

Try it online!

There are 2 common-in-code-golf tasks that are awfully painful in R, and these are base-conversion and manipulating strings. The OP (pajonk) knows this, and is probably smirking right now.

The most straightforward way to perform base-conversion in R would be using integer-division, modulo, and exponentiation, so we could flip 2 hex digits of n like this:
n%/%16^(0:1)%%16%*%16^(1:0).
Unfortunately, though, this doesn't vectorize, so we'd need a costly sapply and additional function definition to run it for each character of the input string in turn.

However, since in this case we know that there are exactly 16x16 possibilities, we can just build a look-up table and use vectorized indexing to retrieve the element with flipped hex digits:
matrix(0:255,16) = 16x16 matrix of the numbers 0:255
t() = transpose it, so the number at each position is the position with its hex digits flipped.
[] = element at that position.

So all we need to do now is get the Utf8 character codes from the input, and convert the looked-up values back to Utf8 characters, frustratingly needing both of the horribly-verbose R functions utf8ToInt() and intToUtf8(). Even with the long function names, this turns-out to be a couple of bytes shorter than only using one of them by building a look-up table directly of characters to match (try it here), which would also require vectors of characters as input instead of true 'strings'.

R, 57 bytes

function(s)intToUtf8(t(matrix(0:255,16))[utf8ToInt(s)+1]) 

Try it online!

There are 2 common-in-code-golf tasks that are awfully painful in R, and these are base-conversion and manipulating strings. The OP (pajonk) knows this, and is probably smirking right now.

The most straightforward way to perform base-conversion in R would be using integer-division, modulo, and exponentiation, so we could flip 2 hex digits of n like this:
n%/%16^(0:1)%%16%*%16^(1:0).
Unfortunately, though, this doesn't vectorize, so we'd need a costly sapply and additional function definition to run it for each character of the input string in turn.

However, since in this case we know that there are exactly 16x16 possibilities, we can just build a look-up table and use vectorized indexing to retrieve the element with flipped hex digits:
matrix(0:255,16) = 16x16 matrix of the numbers 0:255
t() = transpose it, so the number at each position is the position with its hex digits flipped.
[] = element at that position.

So all we need to do now is get the Utf8 character codes from the input, and convert the looked-up values back to Utf8 characters, frustratingly needing both of the horribly-verbose R functions utf8ToInt() and intToUtf8().

R, 57 bytes

function(s)intToUtf8(t(matrix(0:255,16))[utf8ToInt(s)+1]) 

Try it online!

There are 2 common-in-code-golf tasks that are awfully painful in R, and these are base-conversion and manipulating strings. The OP (pajonk) knows this, and is probably smirking right now.

The most straightforward way to perform base-conversion in R would be using integer-division, modulo, and exponentiation, so we could flip 2 hex digits of n like this:
n%/%16^(0:1)%%16%*%16^(1:0).
Unfortunately, though, this doesn't vectorize, so we'd need a costly sapply and additional function definition to run it for each character of the input string in turn.

However, since in this case we know that there are exactly 16x16 possibilities, we can just build a look-up table and use vectorized indexing to retrieve the element with flipped hex digits:
matrix(0:255,16) = 16x16 matrix of the numbers 0:255
t() = transpose it, so the number at each position is the position with its hex digits flipped.
[] = element at that position.

So all we need to do now is get the Utf8 character codes from the input, and convert the looked-up values back to Utf8 characters, frustratingly needing both of the horribly-verbose R functions utf8ToInt() and intToUtf8(). Even with the long function names, this turns-out to be a couple of bytes shorter than only using one of them by building a look-up table directly of characters to match (try it here), which would also require vectors of characters as input instead of true 'strings'.

Source Link
Dominic van Essen
  • 37.2k
  • 2
  • 24
  • 61

R, 57 bytes

function(s)intToUtf8(t(matrix(0:255,16))[utf8ToInt(s)+1]) 

Try it online!

There are 2 common-in-code-golf tasks that are awfully painful in R, and these are base-conversion and manipulating strings. The OP (pajonk) knows this, and is probably smirking right now.

The most straightforward way to perform base-conversion in R would be using integer-division, modulo, and exponentiation, so we could flip 2 hex digits of n like this:
n%/%16^(0:1)%%16%*%16^(1:0).
Unfortunately, though, this doesn't vectorize, so we'd need a costly sapply and additional function definition to run it for each character of the input string in turn.

However, since in this case we know that there are exactly 16x16 possibilities, we can just build a look-up table and use vectorized indexing to retrieve the element with flipped hex digits:
matrix(0:255,16) = 16x16 matrix of the numbers 0:255
t() = transpose it, so the number at each position is the position with its hex digits flipped.
[] = element at that position.

So all we need to do now is get the Utf8 character codes from the input, and convert the looked-up values back to Utf8 characters, frustratingly needing both of the horribly-verbose R functions utf8ToInt() and intToUtf8().