30
\$\begingroup\$

It's one of these again :D

Your task, if you wish to accept it, is to write a program/function (without any uppercase letters) that outputs/returns its string input/argument. The tricky part is that if I convert your source code to uppercase, the output must be reversed.

For simplicity, you can assume that the input is always a single line string containing only ASCII letters (a-z), digits (0-9), and spaces.

You do not need to handle empty input.

Example

Let's say your source code is abc and its input is hello. If I write ABC instead and run it, the output must be olleh.

\$\endgroup\$
14
  • 8
    \$\begingroup\$ How about switching case instead of just uppercasing it? So heLLO would become HEllo. Although this question would still be way too hard \$\endgroup\$
    – user
    Commented Sep 17, 2020 at 16:52
  • 4
    \$\begingroup\$ Anyone feel like using oOo CODE? \$\endgroup\$
    – user
    Commented Sep 17, 2020 at 17:22
  • 11
    \$\begingroup\$ Just a piece of advice: while it's always a good idea to post challenges in the Sandbox (no matter how simple they might seem), it's an extra good idea to do so if something similar has been done multiple times before (such as the "I <blank> the source code, you <blank> the input" challenges) \$\endgroup\$ Commented Sep 17, 2020 at 18:16
  • 3
    \$\begingroup\$ Are the rules the same for letters in other languages like Φ/ϕ? \$\endgroup\$ Commented Sep 17, 2020 at 20:10
  • 3
    \$\begingroup\$ The lowercase rule kicks pip outta this challenge :( \$\endgroup\$
    – Razetime
    Commented Sep 18, 2020 at 4:04

38 Answers 38

1
2
2
\$\begingroup\$

PowerShell, 45 bytes

$args|%{$s+=$_;$r=$_+$r};($s,$r)['a'[0]-ne97]

Try it online!. Outputs 123 Alice.

$ARGS|%{$S+=$_;$R=$_+$R};($S,$R)['A'[0]-NE97]

Try it online!. Outputs ecilA 321.

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

Haskell, 46 bytes

_f[]=[]
_f _s|'a'>'_'=_s|_x:_r<-_s=_f _r++[_x]

Try it online!

_F[]=[]
_F _S|'A'>'_'=_S|_X:_R<-_S=_F _R++[_X]

TRY IT ONLINE!


Regular identifiers in Haskell may not start with an upper-case letter, though starting with an underscore is fine. We cannot use the built-in reverse function, so we implement our own.

f [] = []
f s | 'a'>'_' = s            -- if we are lower-case, just return the string s
    | x:r <- s = f r ++ [x]  -- otherwise, split of the first char x, recursively reverse 
                             -- the remainder r and concatenate it to x
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I used a nearly identical method but came up 8 bytes shorter. Although I did not see your answer prior. \$\endgroup\$
    – Wheat Wizard
    Commented Sep 18, 2020 at 19:06
2
\$\begingroup\$

Javascript 75 bytes

_=>'a'<'['?[..._]['\162\145\166\145\162\163\145']()['\152\157\151\156']``:_

Seems the straightforward way to do it:

  • test if 'a' has become 'A' by now having a lower ascii value than '['
  • if so, break the input into an array, then call .reverse().join('') on it, using octal to avoid spelling out the names of the functions
  • otherwise return the string

Would be a lot shorter if the reversing happened on the lowercase version :)

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

Husk, 9 8 bytes

Edit: -1 byte with non-ASCII case-change thanks to Razetime

?↔o↔↔→ḋ2

Try it online!

If (?) last element () of binary igits of 2 is falsy (it's zero, so it is!), reverse the reverse of the input (o↔↔) - so, output the input unchanged.

Uppercased:

?↔O↔↔→Ḋ2

Try it online!

If (?) last element () of ivisors of 2 is truthy (it's 2, so it is!), reverse input ().


As an alternative that uses the approach of not uppercasing non-ASCII characters, there is also:

?↔+ø→d10

also for 8 bytes (try it)...

\$\endgroup\$
4
  • \$\begingroup\$ looks like there's an uppercase for ø: en.wikipedia.org/wiki/…. \$\endgroup\$
    – Razetime
    Commented Nov 13, 2020 at 14:20
  • \$\begingroup\$ Ah, yes! Thanks (and now noted)! ...but close inspection of the rules indicates that non-ASCII characters could be considered here as non-letters... \$\endgroup\$ Commented Nov 13, 2020 at 14:25
  • \$\begingroup\$ -1 -1(uppercase) \$\endgroup\$
    – Razetime
    Commented Nov 13, 2020 at 14:50
  • \$\begingroup\$ That's cool! Now both situations are covered (=changing case of non-ASCII letters, as well as not-changing case of non-ASCII letters). Thanks! \$\endgroup\$ Commented Nov 13, 2020 at 15:04
1
\$\begingroup\$

Google Sheets, 87 75

Input is in A1.

Lower: A2: =len(a1

Output: =arrayformula(if(code("a")-65,a1,join(,mid(a1,sequence(a2,1,a2,-1),1

Upper: A2: =LEN(A1

Output: =ARRAYFORMULA(IF(CODE("A")-65,A1,JOIN(,MID(A1,SEQUENCE(A2,1,A2,-1),1

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

x86 machine code, 18 bytes

dst=EDI, src=ESI, byte count = ECX. Copies src to dst either forward or reversed.

The only ASCII upper-case byte in the machine code is the 0x71 'q', the opcode for jno, which is always taken (because it follows an xor-zeroing). When lowercased, it becomes 0x51 'Q', a push ecx, which is balanced by a pop ecx in the fall-through path before reaching the loop.

http://ref.x86asm.net/coder32.html#x71 is a useful opcode map. Conveniently, most of the jcc conditional branch opcodes are in the upper-case range.

rev:
    xor   edx, edx
%if 1
    jno   .loop                 ; 71 08  lower case: straight copy
%else
    db 0x51, 0x08               ; JNO becomes 0x51 push ecx, leaving the displacement as an opcode
%endif
    db 0xd1                ; ModRM for the uppercase case, where 08 is an OR opcode
.fallthrough:
    db 0x59                      ;pop  ecx (or RCX in 64-bit mode on TIO)

;    nop                         ; jump displacement needs to be 8, not 7 POP ES which will fault
;    add   edi, ecx
;    dec   edi                   ; so might as well use a longer single instruction
    lea   edi, [edi + ecx - 1]  ; point to *last* byte of destination
    mov   dl, 2
.loop:
    movsb                       ; *edi++ = *esi++
    sub   edi, edx              ; 0 for copy forwards, 2 for copy backwards
    loop  .loop

    ret

Try it online! with a _start that passes buffers and write()s the result.

The code in the fall-through path is 1 byte longer than it needs to be on its own: that makes the jump displacement 08 instead of 07, because opcode 07 is pop es. That segfaults on many values we could push.

And 07 is not valid in 64-bit mode (illegal instruction), otherwise we could possibly arrange a jump that turns into a push of the just-zeroed register, allowing us to pop the null selector into ES. Setting ES to 0 in 32-bit user-space will fault, or lead to movsb faulting. (Even under a 64-bit kernel where 64-bit user-space does run with ds=es=0 null selectors.)

Disassembly of the uppercased version:

08049000 <rev>:
 8049000:       31 d2                   xor    edx,edx
 8049002:       51                      push   ecx
 8049003:       08 d1                   or     cl,dl        # c |= 0  leaves ECX unchanged

08049005 <rev.fallthrough>:
 8049005:       59                      pop    ecx
 8049006:       8d 7c 0f ff             lea    edi,[edi+ecx*1-0x1]
 804900a:       b2 02                   mov    dl,0x2

0804900c <rev.loop>:
 804900c:       a4                      movs   BYTE PTR es:[edi],BYTE PTR ds:[esi]
 804900d:       29 d7                   sub    edi,edx
 804900f:       e2 fb                   loop   804900c <rev.loop>
 8049011:       c3                      ret    

(lower-case ASCII ranges from 'a' 0x61 to 'z' (0x7a). The 7C ModRM for the LEA is just beyond that. 59 pop ecx is already upper case. If there was a case-flip instead of just upper-case version of this challenge, we could make it 0x79. That byte isn't executed in the upper-case version: jno jumps over it.

The lower-case version is:

08049000 <rev>:
 8049000:       31 d2                   xor    edx,edx
 8049002:       71 08                   jno    804900c <rev.loop>
 8049004:       d1                      .byte 0xd1

08049005 <rev.fallthrough>:        # having this label here syncs disassembly after the stray byte above
 8049005:       59                      pop    ecx
 8049006:       8d 7c 0f ff             lea    edi,[edi+ecx*1-0x1]
 804900a:       b2 02                   mov    dl,0x2

0804900c <rev.loop>:
 804900c:       a4                      movs   BYTE PTR es:[edi],BYTE PTR ds:[esi]
 804900d:       29 d7                   sub    edi,edx
 804900f:       e2 fb                   loop   804900c <rev.loop>
 8049011:       c3                      ret    
\$\endgroup\$
1
\$\begingroup\$

Pascal (FPC), 128 bytes

The procedure just prints each character recursively, but switches the printing to after the recursive call if in uppercase mode to reverse the output.

procedure f;var c:char;begin if eof then exit;read(c);if byte('a')-97<0 then begin f;write(c);end else begin write(c);f;end;end;

Try it online!

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

Self-modifying Brainfuck, 52 bytes

represents the literal null byte \x00

-[<->---]<[[-]<[<]>----------------[>]]>,[␀>,]<[.<]u

Try it online!

On the interpreter the input has a null byte at the end

EOF is \x00

\$\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.