13
\$\begingroup\$

Your function must accept one string and return the opposite

The opposite string is a string where all characters go in reverse order, all letters are replaced with the opposite mirror letter from the English alphabet and are changed in case, and all digits are replaced with opposite mirror digits

opposite mirror for letters means that a must be replaced with z, b must be replaced with y and so on. In general let's say we have some letter which index in alphabet is Index then it must be replaced with letter which index is 25 - Index (25 if zero based)

opposite mirror for digits means that 0 must be replaced with 9, 1 must be replaced with 8 and so on. In general let's say we have some digit which index in 0123456789 is Index then it must be replaced with digit which index is 9 - Index (9 if zero based)

If there is a symbol from non English alphabet or digits in string then you don't change it just move to the correct position in reversed string

Input string can contain any printable character. I mean that there also can be Chinese symbols, Arabic symbols, Russian symbols and so on. But all symbols will be printable

Test cases:

Hello world! --> !WOILD LOOVs

Z 02468 a 13579 A --> z 02468 Z 13579 a

~!@#$%^&*() --> )(*&^%$#@!~

(I) [LOVE] {PROGRAMMING} ,,more,, ..than.. ??10000000000!! --> !!99999999998?? ..MZSG.. ,,VILN,, }tmrnnzitlik{ ]velo[ )r(

By the way мне нравится программировать! --> !ьтавориммаргорп ястиварн енм BZD VSG By

The shortest code in each programming language wins!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ The opposite mirror for letters is just the Atbash cipher. :> \$\endgroup\$
    – Someone
    Feb 17, 2023 at 18:09

16 Answers 16

8
\$\begingroup\$

MATL, 22 14 12 bytes

P8Y2tP10YSXE

Try it at MATL Online

Explanation

           % Implicitly grab the input as a string
P          % Reverse the input
8Y2        % Push the predefined literal ['0:9', 'A:Z', 'a:z'], a lookup array
t          % Duplicate this array ... 
P          % Reverse ...
10YS       % And circularly shift by 10 ...
           % to construct the replacement array ['9:0', 'z:a', 'Z:A']
XE         % Perform the translation
           % Implicitly display the result
\$\endgroup\$
6
\$\begingroup\$

Vyxal, 9 7 bytes

- 2 bytes by AndrovT porting Suevers MATL answer

ṘkrḂ₀ǔĿ

No questionable constants :p
Try it Online!

How it works:

ṘkrḂ₀ǔĿ
Ṙ           Reverse string
 krḂ        Bifurcate [0-9A-Za-z], (duplicate and reverse)
    ₀ǔ      Shift string by 10 [9-0z-aZ-A]
      Ŀ     Transliterate
\$\endgroup\$
1
  • \$\begingroup\$ Port of the MATL answer is 7 bytes \$\endgroup\$
    – AndrovT
    Feb 17, 2023 at 23:13
5
\$\begingroup\$

Bash + common Linux utils, 53

printf -vA %s {Z..A} {9..0}
tr a-z0-9A-Z $A${A,,}|rev

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Pip, 26 bytes

RaRXA(Yz.AZ)@(Ry@?_)RXD9-_

Try It Online!

Regex-based solution, as pip doesn't have bifurcation (that I could find). Annoyingly, there is no variable for lowercase and uppercase alphabet in pip, so it had to be constructed manually. Explanation:

R                       # Reverse
 aR                     # The input regex replaced with
   XA                   # The pattern [A-Za-z]
     (Y                 # Yank (store in variable y) the
       z.AZ)            # alphabetical iterable (a-z + A-Z)
            @(          # at the index of
                @?_)    # the matched character in
              Ry        # the reverse alphabetical iterable
  R                     # Regex replace again with
   XD                   # Regex for digits
     9-_                # Mirrored digit
                        # Implicit output
\$\endgroup\$
4
\$\begingroup\$

05AB1E,  9  8 bytes

RžKÂT._‡

Try it online!

Port of Suever's MATL answer.
(Saved a byte by porting it)

Explanation

RžKÂT._‡  # Implicit input
R         # Reverse
 žK       # Push [a-zA-Z0-9]
   Â      # Bifurcate
    T._   # Shift by 10 
       ‡  # Transliterate
          # Implicit output
\$\endgroup\$
3
\$\begingroup\$

JavaScript (Node.js),77 76 bytes

f=([c,...x])=>c?f(x)+Buffer(c).map(t=>t+c>t?57-c:/[a-z]/i.test(c)?187-t:t):x

Try it online!

\$\endgroup\$
0
3
\$\begingroup\$

Python, 86 bytes

lambda s:''.join(chr(abs((47<a<58)*105+(96<a|32<123)*187-a))for a in map(ord,s[::-1]))

Attempt This Online!

\$\endgroup\$
2
\$\begingroup\$

Retina 0.8.2, 15 bytes

T`dLld`Ro
O^$`.

Try it online! Link includes test cases. Explanation:

T`dLld`Ro

Transliterate the characters in the string 0..9A..Za..z0..9 to the characters in the reverse of that string.

O^$`.

Reverse the string.

\$\endgroup\$
2
\$\begingroup\$

Charcoal, 22 bytes

≔⪫⪪⭆χι⁵⁺αβθ⭆⮌S§⁺⮌θι⌕θι

Try it online! Link is to verbose version of code. Explanation:

≔⪫⪪⭆χι⁵⁺αβθ

Build up the transliteration string 01234A..Za..z56789 by taking the digits, splitting into two substrings of length 5, then joining them with the upper and lower case letters.

⭆⮌S§⁺⮌θι⌕θι

Map over the reversed input string, finding each character in the transliteration string and looking up the character in the same position in the reversed transliteration string, but with the original character appended so that if it wasn't found then cyclic indexing picks up the original character.

\$\endgroup\$
2
\$\begingroup\$

Python, 105 103 bytes

lambda s:s.translate([47<c<57and 105-c or(64<c<91or 96<c<123)and 187-c or c for c in range(128)])[::-1]

Attempt This Online!

-2 thanks to @Mukundan314

\$\endgroup\$
1
2
\$\begingroup\$

Factor + spelling, 88 86 bytes

[ reverse ALPHABET "0123456789"over >upper glue dup reverse 10 rotate zip substitute ]

Try it online!

Port of Suever's MATL answer.

\$\endgroup\$
2
\$\begingroup\$

Python, 86 bytes

lambda S:S.translate(dict(zip(A[65:],A[::-1])))[::-1]
*A,=range(123)
A[91:97]=A[48:58]

Attempt This Online!

\$\endgroup\$
2
\$\begingroup\$

Zsh --braceccl --extendedglob, 86 bytes

M=({a-z} {0-9} {A-Z})
<<<${(j..)${(Oas..)1//(#m)?/${M[1+$#M-$M[(Ie)$MATCH]]:-$MATCH}}}

Try it online!

Find the (I) last index in the array, count from the end to find the replacement, :- fallback to the original letter if none was found.

\$\endgroup\$
2
\$\begingroup\$

C (gcc), 135 bytes

In order for UTF-8 characters outside of the ASCII range to not turn into gibberish when reversed I used ICU's functions to iterate the string.

#import<unicode/utf8.h>
f(s,i,t)char*s;{i=0;U8_NEXT(s,i,0,t);*s&&f(s+i),isalnum(*s)?putchar(187-82*isdigit(*s)-*s):printf("%.*s",i,s);}

Try it online!

Ungolfed:

#import<unicode/utf8.h>
f(s,i,t)char*s;{
  i=0;U8_NEXT(s,i,0,t); // Get the next codepoint
  *s&& // Terminate recursion at end of string
  f(s+i), // Run the string in reverse
  isalnum(*s)? // Is [0..9,A..Z,a..z]?
    putchar(187-82*isdigit(*s)-*s): // Yes, invert character
    printf("%.*s",i,s); // Print uninverted codepoint as UTF-8
}
\$\endgroup\$
1
\$\begingroup\$

JavaScript, 109 bytes

s=>String.fromCharCode(...[...s].reverse().map(c=>/[a-z]/i.test(c,n=c.charCodeAt())?187-n:n<48|n>57?n:105-n))

Try it:

f=s=>String.fromCharCode(...[...s].reverse().map(c=>/[a-z]/i.test(c,n=c.charCodeAt())?187-n:n<48|n>57?n:105-n))

;[
    'abcdefghijklmnopqrstuvwxyz',
    'Hello world!',
    'Z 02468 a 13579 A',
    '~!@#$%^&*()',
    '(I) [LOVE] {PROGRAMMING} ,,more,, ..than.. ??10000000000!!',
    'By the way мне нравится программировать!'
].forEach(s => console.log(f(s)))

\$\endgroup\$
0
\$\begingroup\$

Befunge-98 (FBBI), 136 bytes

>]>:88*`v>:68*1-` v
^~v*8f3:_^v`+2*78:_,:!#@_
^<>+\`#v_^>!#v_,:!#@_
v`*8c::<
_7d*\`#v_^v$;>01-*v;
 vf*-10<  <v+**753<
v>2+b*+    >,:!#@_

Try it online!

Not a hugely golfed answer, and probably invalid due to lack of handling for Unicode, but a fun exercise!

\$\endgroup\$

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.