63
\$\begingroup\$

Challenge

Given a non-empty string S of length L consisting entirely of printable ASCII chars, output another string of length L that consists entirely of printable ASCII chars, but is not equal to S.

For the purposes of this challenge, a printable ASCII char is one between U+0020 and U+007E, inclusive; that is, from (space) to ~ (tilde). Newlines and tabs are not included.

For example, given "abcde", some valid outputs could be:

  • "11111"
  • "abcdf"
  • "edcba"

But these would be invalid:

  • "abcde"
  • "bcde"
  • "abcde0"

Test cases

"asdf"
"1111"
"       "
"~~~~~"
"abcba"
"1"
" "
"~"
" ~"
"~ "
"  0"
"!@#$%^&*()ABCDEFGhijklmnop1234567890"
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"

Rules

  • You may assume the input consists entirely of printable ASCII chars.
  • You may not assume that the input does not contain all 95 printable chars.
  • You may assume the input contains at least one character and is less than 256 chars long.
  • The output must also consist entirely of printable ASCII chars. You could not, for example, output the byte \x7F for input "~".
  • The output must be different than the input with probability 1; that is, you may generate random strings until one is different than the input, but you can't just output L random characters and hope it's different.
  • Newlines are disallowed in the output, but you may output one trailing newline which is not counted toward the string.

Scoring

This is , so the shortest code in bytes in each language wins.

\$\endgroup\$
7
  • \$\begingroup\$ Note that "positive" excludes the empty string. For extra clarity, maybe replace "positive" with "nonzero"? \$\endgroup\$ Commented May 30, 2017 at 20:30
  • 6
    \$\begingroup\$ @CalculatorFeline But that would include negative-length strings /s \$\endgroup\$ Commented May 30, 2017 at 20:36
  • 1
    \$\begingroup\$ ...Those don't exist. \$\endgroup\$ Commented May 30, 2017 at 20:38
  • \$\begingroup\$ @CalculatorFeline Better now? \$\endgroup\$ Commented May 30, 2017 at 20:40
  • 3
    \$\begingroup\$ Another simple but not trivial challenge. \$\endgroup\$ Commented Apr 4, 2018 at 15:03

77 Answers 77

1 2
3
0
\$\begingroup\$

SAS, 34

substr(s,1,1)=ifc(s=:'z','a','z');

If the first character of the string s is z, change it to a, otherwise change it to z. Rubbish compared to the proper golfing languages, but at least it beats C#!

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

T-SQL, 45 bytes

SELECT STUFF(S,1,1,CHAR(ASCII(S)%7+48))FROM t

Takes input from pre-existing table t with VARCHAR field S, per our IO standards.

Modifies the first character by taking the remainder MOD 7 of the ASCII value, then converting that back into a digit between 0 and 6. But ASCII(0)=48, which is 6 MOD 7, so the first character never maps back onto itself.

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

Cubically, 23 bytes

(~-61/1=7&6:7>4?6@4!@5)

Converts the string into $s and -s.

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

Implicit, 15 14 bytes

'_._` !{+32}*-

Found about 10 interpreter bugs writing this. Try it online!

'_._` !{+32}*-
'                « read string                              »;
 _               « push last char onto stack                »;
  .              « increment                                »;
   _`            « mod by 127 (the "` " evaluates to 127)   »;
      !{...}     « if falsy (char was ~)                    »;
        +32      «  add 32 (make it a space)                »;
            *    « stick char at beginning of string        »;
             -   « pop last char off string                 »;
\$\endgroup\$
0
\$\begingroup\$

Charcoal, 8 bytes

FSIX⁰⁼ι1

Try it online! Link is to verbose version of code. Works by comparing each character to 1, then taking the result as the exponent of zero, then converting to string. This results in an output 0 for each 1 in the input and a 1 for each other character. Edit: Changes in Charcoal now mean that I can directly cast the result of Equals saving 2 bytes: FSI⁼ι0 Try it online! Link is to verbose version of code. Edit: Actually there was a 6-byte solution all along: FS§α℅ι Try it online! Link is to verbose version of code. Port of @FlipTack's Pushy answer. (Also works with lower case of course.)

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

BrainFuck, 49 47 Bytes

,>++++[-<-------->]++[<]++++[->++++++++<]>-[.,]
x           0   (x-32) 2                (x-32||2)+31
^           ^(x>32) ^(x==32)

Assuming EOF as zero

\$\endgroup\$
2
  • \$\begingroup\$ You can cut out the first set of ., and just go straight into the loop \$\endgroup\$
    – Jo King
    Commented Dec 26, 2017 at 15:54
  • \$\begingroup\$ Thanks \$\endgroup\$
    – l4m2
    Commented Dec 27, 2017 at 0:26
0
\$\begingroup\$

Perl 5, 23 + 1 (-p) = 24 bytes

s/./chr 65+ord($&)%26/e

Try it online!

Blatantly steals @FlipTack's method to change the first character to a different upper case letter.

\$\endgroup\$
1
  • \$\begingroup\$ You can drop one of the digits in 26, at least. \$\endgroup\$ Commented Jan 11, 2018 at 0:51
0
\$\begingroup\$

Fortran (GFortran), 77 bytes

CHARACTER(256)A
READ*,A
A(1:1)=CHAR(MOD(IACHAR(A(1:1))+1,94)+33)
PRINT*,A
END

Try it online!

It does what was required... well, with some issues...

1) If the first character is "or <space>, they will be ignored;

2) If there is /, it will be understood as something like "end of transmission";

3) There will be a leading space (which is not part of the output (so do I think), because Fortran always prints a leading space if the output is not formated) and up to 255 trailing spaces. This last problem (i.e., the trailing spaces) can be solved using no more then six bytes.

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

SNOBOL4 (CSNOBOL4), 71 bytes

	INPUT LEN(1) . X REM . S
	X =IDENT(X) 1	:S(O)
	X =0
O	OUTPUT =X S
END	

Try it online!

Replaces the first character of S with a 0 unless it's already, 0 in which case it swaps it with 1.

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

Pyth, 12 bytes

+C+%ChQlG32t

Test suite

Python 3 translation:
Q=eval(input())
print(chr(ord(Q[0])%26+32)+Q[1:])
\$\endgroup\$
0
\$\begingroup\$

Swift 3, 76 68 bytes

let s=readLine()!;print(s.characters.map{$0=="a" ?"b":"a"}.joined())

Try it online!

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

Tcl, 56 bytes

proc C s {join [lmap x [split $s ""] {expr {$x==0}}] ""}

Try it online!

Approach very similar to last one: replaces every non-zero by a zero and every zero by one.


Tcl, 62 bytes

proc C s {puts [expr {[string in $s 0]==0}][string ra $s 1 e]}

Try it online!

Approach replaces first character by 1 if it is a 0, else it replaces anything else by a 0.

Tcl, 83 bytes

proc C s {scan [string in $s 0] %c f
puts "[format %c [incr f]][string ra $s 1 e]"}

Try it online!

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

naz, 36 bytes

2a2x1v1x1f1r3x1v2e5p1o1f0x1x2f0a0x1f

Works for any input string terminated with the control character STX (U+0002).

Explanation (with 0x commands removed)

2a2x1v             # Set variable 1 equal to 2
1x1f               # Function 1
    1r             # Read a byte of input
      3x1v2e       # Jump to function 2 if it equals variable 1
            5p1o   # Otherwise, output it modulo 5
                1f # Jump back to the start of function 1
1x2f               # Function 2
    0a             # Add 0 to the register
1f                 # Call function 1
\$\endgroup\$
0
\$\begingroup\$

Thunno J, \$ 5 \log_{256}(96) \approx \$ 4.12 bytes

O2%1_

Attempt This Online!

Port of Riley's 05AB1E answer.

Explanation

O2%1_  # Implicit input
O      # Ordinals of input
 2%    # Mod 2 (vectorised)
   1_  # Not (vectorised)
       # J flag joins the list
       # Implicit output
\$\endgroup\$
0
\$\begingroup\$

MathGolf, 4 bytes

É1░¡

Converts 1 to 0 and every other character to 1.

Try it online.

Explanation:

É     # Foreach over each character of the (implicit) input-string,
      # using 3 characters as inner code-block:
 1░   #  Push 1, and convert it to a string: "1"
   ¡  #  Check whether it's NOT equal to the current character
      # (after which the entire stack is joined together and output implicitly)
\$\endgroup\$
0
\$\begingroup\$

Thunno 2 JB, 1 byte

E

Attempt This Online!

Thunno 2, 3 bytes

CEJ

Attempt This Online!

Port of Riley's 05AB1E answer. Converts to charcodes and checks if even. Then, joins the resulting list.

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

(,) N, 116 108 Chars or \$108\log_{256}(3)\approx21.4\$ Bytes

((),()()()()()()())(,,,,(()(),((()()())))(()(),(),(),,(()()),()((),,,,(),,(())))(()(),(),()(),,(()()),()()))

Try It Online!
Or, without flags, for 138 chars or 27.34 Bytes

((),()()()()()()()()()())(,,,,(()(),((()()())))(()(),(),,(())(())(())()(),(()()),(())(())(())(()))(()(),(),,(())(())(())(()),(()()),()()))

Try It Online!
Basically, it outputs 1 (with flags) or \32 (without flags) if the char code is greater than or equal to that of \50 (with flags) or \40 (without flags), and outputs 2 (with flags) or \40 (without flags) otherwise.
The reason 1 and 2 are written as 1 and 2 rather than as \49 and \50 is because they are outputted numerically, hence the need for the N flag to remove seperators. All others are encoded in the code by char code.

\$\endgroup\$
1 2
3

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.