| Bytes | Lang | Time | Link |
|---|---|---|---|
| 122 | SAKO | 250327T161620Z | Acrimori |
| 039 | Swift 6 | 250302T003237Z | macOSist |
| 055 | Tcl | 181226T204434Z | sergiol |
| nan | Pxem | 240618T135713Z | 鳴神裁四点一号 |
| nan | 230611T030811Z | Dadsdy | |
| 001 | Thunno 2 JB | 230610T161846Z | The Thon |
| 004 | MathGolf | 230308T095752Z | Kevin Cr |
| 008 | APL Dyalog Extended | 230302T131904Z | The Thon |
| nan | 230302T132513Z | The Thon | |
| 003 | Vyxal | 220105T232837Z | emanresu |
| 006 | Husk | 201118T103452Z | Dominic |
| 036 | naz | 200907T071918Z | sporebal |
| 011 | MAWP | 200819T072752Z | Razetime |
| 026 | PowerShell | 181227T234037Z | Veskah |
| 025 | SmileBASIC | 180404T235408Z | 12Me21 |
| 022 | C gcc | 180412T044505Z | MegaTom |
| 026 | Python 3 | 180404T074859Z | xbarbie |
| 003 | ><> | 171230T012224Z | Jo King |
| 040 | R | 180405T074744Z | Sean Rob |
| 006 | Gol><> | 180406T171849Z | Bubbler |
| 002 | str | 180404T233322Z | Conor O& |
| 009 | Pyth | 180404T151759Z | user4854 |
| 014 | Perl 6 | 180404T135142Z | Phil H |
| 042 | BrainFlak | 180404T111242Z | Jo King |
| 068 | Swift 3 | 180403T164909Z | user3151 |
| 012 | Pyth | 180403T225632Z | hakr14 |
| 071 | SNOBOL4 CSNOBOL4 | 180403T192434Z | Giuseppe |
| 077 | Fortran GFortran | 180403T190340Z | rafa1111 |
| 038 | Java 8 | 180403T091521Z | Kevin Cr |
| 026 | Julia 0.6 | 180111T000323Z | gggg |
| nan | Perl 5 | 180110T232311Z | Xcali |
| 001 | Pushy | 171226T123558Z | FlipTack |
| 006 | x86 opcode | 171230T083420Z | l4m2 |
| 027 | brainfuck | 171226T155327Z | Jo King |
| 047 | BrainFuck | 171226T145041Z | l4m2 |
| 029 | C/C++ | 171226T133953Z | HatsuPoi |
| 008 | Charcoal | 170531T085713Z | Neil |
| 022 | Funky | 171121T025144Z | ATaco |
| 014 | Implicit | 170906T003843Z | MD XF |
| 003 | Golfscript | 170906T015526Z | Josiah W |
| 049 | Perl 5 | 170820T231103Z | Ed. |
| 023 | Cubically | 170809T233203Z | TehPers |
| 045 | TSQL | 170623T165529Z | BradC |
| 019 | x86 Assembly | 170623T155523Z | Govind P |
| 034 | SAS | 170602T103855Z | user3490 |
| 025 | Python 2 | 170530T160733Z | xnor |
| 005 | Chip | 170531T195509Z | Phlarx |
| 031 | PHP<7.1 | 170530T165400Z | Jör |
| 046 | C# | 170531T111502Z | LiefdeWe |
| 020 | C gcc | 170531T104230Z | Ørj |
| 012 | Braingolf | 170531T100057Z | Mayube |
| 009 | Brachylog | 170531T065336Z | Fatalize |
| 009 | Alice | 170531T014850Z | Nitrodon |
| 022 | C gcc | 170530T224625Z | Dennis |
| nan | Ruby | 170530T222811Z | Value In |
| 010 | Cubix | 170530T215413Z | ETHprodu |
| 019 | JavaScript ES6 | 170530T161556Z | Stephen |
| 027 | PHP | 170530T204150Z | Christop |
| 004 | Japt | 170530T201206Z | Oliver |
| 007 | V | 170530T170922Z | DJMcMayh |
| 011 | ><> | 170530T185847Z | Luis Men |
| 024 | JavaScript ES6 | 170530T190814Z | Neil |
| 009 | Aceto | 170530T183213Z | L3viatha |
| 019 | Haskell | 170530T182827Z | Ørj |
| 004 | Convex | 170530T181308Z | GamrCorp |
| 003 | Jelly | 170530T172832Z | Dennis |
| 021 | Python 2 | 170530T170906Z | xnor |
| 013 | Bash + coreutils | 170530T173540Z | Digital |
| 003 | 05AB1E | 170530T173414Z | Riley |
| 004 | Jelly | 170530T165921Z | Dennis |
| nan | 170530T170349Z | Brad Gil | |
| 005 | MATL | 170530T161626Z | Luis Men |
| 020 | Haskell | 170530T161518Z | xnor |
| 059 | Whitespace | 170530T163155Z | Censored |
| 006 | Retina | 170530T160100Z | user4180 |
| 005 | CJam | 170530T161641Z | Martin E |
| 018 | Octave | 170530T160602Z | Stewie G |
| 053 | BrainFlak | 170530T161411Z | Riley |
| 026 | Haskell | 170530T160720Z | nimi |
| 005 | 05AB1E | 170530T160324Z | Emigna |
SAKO, 122 bytes
PODPROGRAM:F(*T)
CALKOWITE:*T,I
STRUKTURA(2*8):T
I=0
1)T(I)=MOD(T(I),9)+2
I=I+1
GDYT(I)=0:2,INACZEJ1
2)DRUKUJWIERSZ:T
WROC
Works in KW6 encoding, but shouldn't largely matter.
Explanation:
We loop through every element in the array taking modulo of it and 9. Later we add 2 to not have a 0.
We stop when we find 0 as it symbolizes the end of the string (we may know that, because we assume that the string is less than 256 characters long, which is how large is our array.
Full programme version, 131 bytes
CALKOWITE:*T,I
BLOK(2*8):T
T(=I)=0
CZYTAJWIERSZ:T
I=0
1)T(I)=MOD(T(I),9)+2
I=I+1
GDYT(I)=0:2,INACZEJ1
2)DRUKUJWIERSZ:T
STOP1
KONIEC
Swift 6, 39 bytes
{String(($0+"").map{$0=="#" ?"@":"#"})}
This is fairly trivial. For each character in $0, we return "@" if it's a hash, or "#" otherwise. (You could replace those with any two distinct characters, I just had Swift macros on my mind while writing this.)
Tcl, 55 bytes
proc C s {join [lmap x [split $s ""] {expr {$x<1}}] ""}
Approach very similar to last one: replaces some characters different from one by one, and some other characters different from zero by zero.
proc C s {join [lmap x [split $s ""] {expr {$x==0}}] ""}
Approach very similar to last one: replaces every non-zero by a zero and every zero by one.
proc C s {puts [expr {[string in $s 0]==0}][string ra $s 1 e]}
Approach replaces first character by 1 if it is a 0, else it replaces anything else by a 0.
---proc C s {scan [string in $s 0] %c f
puts "[format %c [incr f]][string ra $s 1 e]"}
Pxem, 27 + 0 = 27 bytes
Filename (\1 is a codepoint 1)
.w.i.c}.x.s!! .a.c\1.+.a.-.p
Content
Try it online! with RPxem.
How it works
.w .a
Loop while stack is empty or (1) pop a value. Then (2) the value is nonzero.
.i
Read a character. EOF is -1.
.c}.x.s!! .a
If the character is greater than codepoint of } (~ is the only one) replace it with . !! is to get out of the loop between .x and .a.
.w .c\1.+.a
Get out of the loop when EOF is received; dup, push 1, add. Loop while nonzero.
.-.p
Pop two values to push its difference; which is last non-eof minus -1. Finally output every stack character.
Tldr: reverse entire input. The final character is incremented. However every occurrence of tilde ~ is replaced with ; but last ~ is with !.
Future direction
- Reimplement Pxem so you can try on browser
(,) 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.
Thunno 2 JB, 1 byte
E
Thunno 2, 3 bytes
CEJ
Port of Riley's 05AB1E answer. Converts to charcodes and checks if even. Then, joins the resulting list.
MathGolf, 4 bytes
É1░¡
Converts 1 to 0 and every other character to 1.
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)
APL (Dyalog Extended), 11 8 bytes
∊⍕¨'0'=⎕
-3 thanks to @Adám
Explanation
∊⍕¨'0'=⎕
'0'=⎕ For each character: 1 if it's '0', 0 otherwise
⍕¨ Convert each to string
∊ Flatten into a single string
Thunno J, \$ 5 \log_{256}(96) \approx \$ 4.12 bytes
O2%1_
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
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
MAWP, 11 bytes
0|[!5P5WA:]
Idea from Jo King's answer.
Prints the ASCII value of each letter modulo 5.
PowerShell, 37 26 bytes
Thanks Mazzy for -11 bytes
-join($args|% t*y|%{$_%9})
Does alright. Uses the ToCharArray trick to save some bytes. Through a conversion chain, mods the ASCII value of each character by 9 then joins up the value.
SmileBASIC, 27 25 bytes
DEF D S
S[0]=@1[S>"2"]END
Creates a function named D that is called like:
STRING$="abcde"
D STRING$
'STRING$ is now "1bcde"
The first character is replaced with either @ or 1
Python 3, 29 26 bytes
lambda s:'a~'[s<'~']+s[1:]
- Replacing first character with
~, if the first character is~already, it is replaced bya.
><>, 9 3 bytes
5%n
Takes input through the -s flag. Mods each digit with 5 and prints the integer, ending when the stack is empty. This results in only 5 possible output characters, 0-4. Each of those numbers when inputted will produce themselves plus 3, as the ASCII value of 0 is 48, which mod 5 is 3. While the output is reversed, no number is equivalent to another.
Both 7, 9 and a(10) also work as they are not factors of 48 and print single digits.
R, 40 bytes
function(s)substr(shQuote(s),1,nchar(s))
This uses the shell quoting function shQuote to place appropriate quotes around a string and escape quotes within the string (similar to @xnor's answer). Then take a substring that's as long as the original string (because the string might have multiple escaped characters, we need to take a substring of the correct length, so we can't just use substring(...,3)). sQuote is shorter, but converts ‘ to ‘‘’.
Another approach, using more 'core' R stuff is 46 bytes:
function(s)sub(".",letters[grepl("^a",s)+1],s)
Substitute the first character with 'a', unless it is 'a', then substitute it for 'b'. sub only substitutes the first match, so we don't need ^. letters is constant and a slightly shorter way of doing c("a","b"). grepl returns a boolean, and adding 1 converts it to a number that indexes letters. The ^ is needed to correctly convert "ba".
Code to test
x = function(s)substr(shQuote(s),1,nchar(s))
test = function(t){
t2 = x(t)
t2!=t & nchar(t)==nchar(t2)
}
testStrings = c("a","b","ab","ba","aa",
"'","'\"","\\\\",'"','""',
'‘','’',"~","asdf",
"1111"," ","~~~~~","abcba",
"1"," ","~"," ~","~ "," 0",
"!@#$%^&*()ABCDEFGhijklmnop1234567890",
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~")
all(sapply(testStrings,test))
str, 2 bytes
XV
Explanation
XV
over each character:
X increment it ("~" becomes "\x7f" and "@" becomes "A")
V wrap to printable ascii ("\x7f" becomes " ")
Alternatively, YV to decrement instead of increment.
Pyth, 9 bytes
*h-+QGhQl
Explanation
*h-+QGhQl
+QG Stick the alphabet to the end of the input.
- hQ Delete all instances of the first character.
h Take the first remaining character.
* lQ Multiply by the length of the (implicit) input.
Perl 6, 14 bytes
{.ord%7~.chop}
Bare block, implicit lambda with input $_. The ordinal of the first character .ord modulo 7 is a different character: Non-numeric first character becomes numeric, numeric first characters map to a different character. If we append the input sans the last character (~ $_.chop), we match the length. It can never match the input because the first character never matches.
Perl 6, 15 bytes
{S/./{.ord%7}/}
Performs S/// substitution (which returns the result of substituting on $_), matching . (any character) once, and replacing it with the ordinal of the first character in $_ - the same character - modulo 7. Non-numeric first character becomes numeric, numeric first characters map to a different character.
Brain-Flak, 42 bytes
({}[((((()()()()){}){}){})]<>){{}}([]<>{})
Replaces the first character of the string with a space, or a ! if the first character is already a space.
Swift 3, 76 68 bytes
let s=readLine()!;print(s.characters.map{$0=="a" ?"b":"a"}.joined())
Pyth, 12 bytes
+C+%ChQlG32t
Python 3 translation:
Q=eval(input())
print(chr(ord(Q[0])%26+32)+Q[1:])
SNOBOL4 (CSNOBOL4), 71 bytes
INPUT LEN(1) . X REM . S
X =IDENT(X) 1 :S(O)
X =0
O OUTPUT =X S
END
Replaces the first character of S with a 0 unless it's already, 0 in which case it swaps it with 1.
Fortran (GFortran), 77 bytes
CHARACTER(256)A
READ*,A
A(1:1)=CHAR(MOD(IACHAR(A(1:1))+1,94)+33)
PRINT*,A
END
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.
Java 8, 39 38 bytes
s->(s.charAt(0)<49?9:0)+s.substring(1)
Explanation:
If the first character has a unicode value below 49 (space, or one of !"#$%&'()*+'-./0), change it to a '9', else change it to a '0'.
s-> // Method with String as both parameter and return-type
(s.charAt(0)<49? // If the first character has a unicode value below 49:
9 // Change the first character to a '9'
: // Else
0) // Change the first character to a '0'
+s.substring(1) // And return the rest of the input-String (if any)
Perl 5, 23 + 1 (-p) = 24 bytes
s/./chr 65+ord($&)%26/e
Blatantly steals @FlipTack's method to change the first character to a different upper case letter.
Pushy, 1 byte
Q
This converts the given string into list of ASCII character codes, indexes them (modular indexing) into the uppercase alphabet, then prints the result. Essentially, each character n is mapped to chr(ord(n) % 26 + 65). You can use this program to see how the mapping works.
The output:
- Will always be the same length as the input, as the characters are directly mapped into new ones.
- Will always comprise only printable ASCII characters (as it only contains uppercase letters)
- Will always be different to the input, as there is no possible input character
nsuch thatchr(ord(n) % 26 + 65) == n, as for this to be true there must be an integerxsuch that26x = 65, for which there is no solution.
1 byte
q
This answer is exactly the same, except that it maps to lowercase alphabet characters rather than uppercase alphabet characters. This is still valid as there is no possible input character n such that chr(ord(n) % 26 + 97) == n.
x86 opcode, 6 bytes
and byte [ecx], 0xF1
inc byte [ecx]
ret
brainfuck, 35 27 bytes
Edit: switched to a better way of generating an odd number thanks to the brainfuck constants page
,[[[>]+<[-<]>]-[>+<---]>.,]
Prints out a string of semi-random Vs and Us. If the first character of the input is a V it will print out a U instead and vice versa.
How it Works
,[ Gets first inputted char
[[>]+<[-<]>] Converts the ASCII value to binary
-[>+<---] Adds 85 to the last digit of that binary, which is the original value mod 2.
This converts a V (value 86) into a U (value 85) and vice versa
>. Print the value
,] Get a new value and repeat.
Note that the binary of the previous value is still in memory,
so it no longer prints specific chars for odd or even values
Another version with the same bytecount:
,[[>]+<[-<]>]-[>+<---]>[.,]
Only changes the first character to a V or a U, but doesn’t handle empty input (which is still valid)
BrainFuck, 49 47 Bytes
,>++++[-<-------->]++[<]++++[->++++++++<]>-[.,]
x 0 (x-32) 2 (x-32||2)+31
^ ^(x>32) ^(x==32)
Assuming EOF as zero
C/C++, 29 bytes
void m(char*s){(*s%=26)+=65;}
Code to test :
#include <iostream>
#include <string>
#include <cassert>
#ifdef _MSC_VER
#pragma warning(disable:4996)
#endif
void m(char*s){(*s%=26)+=65;}
int main() {
std::initializer_list<std::string> test{
"asdf",
"1111",
" ",
"~~~~~",
"abcba",
"1",
" ",
"~",
" ~",
"~ ",
" 0",
"!@#$%^&*()ABCDEFGhijklmnop1234567890",
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
};
for (auto&a : test) {
char* t = new char[a.size() + 1];
std::strcpy(t, a.c_str());
m(t);
assert(a != t);
std::cout << a << " => " << t << '\n';
delete[] t;
}
}
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.)
Funky, 26 22 bytes
s=>s::gsub("."a=>1&~a)
Calculates ~a which for non-digits will return NaN. Then 1& limits it to either 0 or 1, for the digit 0, this will be 1, and for 1 this will be 0. So this string is always unique.
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 »;
Golfscript, 3 bytes
)5%
Take the last character's ASCII value modulo 5, and replace the last character with the result. This obviously works for non-digit characters, but if the last character is a digit, it also changes ("0" mod 5 = 3, "1" mod 5 = 4, etc).
This would also work with 7 or 9 while still keeping the same length.
Also, yay! I have a Golfscript solution as good as the best solutions here!
Perl 5, 49 bytes
sub{$_="a"x length $_[0];$_++while $_ eq$_[0];$_}
Explanation:
- starts with string of letter "a" as long as input
- keeps incrementing that until it's different from input
Starting from "a"s was to avoid incrementing to point where Perl lengthens the string; with only one strings to avoid being same as, it couldn't overflow.
This is shamelessly cribbed (and simplified) from my answer to the "related" problem "The Third String".
Execute with:
perl -e '$s = ' -E 'sub{$_="a"x length $_[0];$_++while $_ eq$_[0];$_}' -E ';say $s->("z")'
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.
x86 Assembly, 19 bytes
Alternates the first character of the string between '@' and 'A'
_diffstr:
00000000: 8B 54 24 04 mov edx,dword ptr [esp+4]
00000004: 8A 02 mov al,byte ptr [edx]
00000006: 3C 41 cmp al,41h
00000008: 75 04 jne 0000000E
0000000A: B0 40 mov al,40h
0000000C: EB 02 jmp 00000010
0000000E: B0 41 mov al,41h
00000010: 88 02 mov byte ptr [edx],al
00000012: C3 ret
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#!
Python 2, 25 bytes
lambda s:`s<'T'`[0]+s[1:]
Anders Kaseorg saved a byte by extracting the first character from True or False.
Chip, 5 bytes
g*A~a
Maps all characters to either @ or A, so that the lowest bit always differs from the input:
0, @, ~, etc. map to A
1, a, A, etc. map to @
C#, 46 bytes
_=>(char)(_[0]>32?_[0]-1:_[0]+1)+_.Remove(0,1)
Changes first letter to next to previous letter on ASCII table unless it is the first letter, in which case it takes the next one.
C (gcc), 20 bytes
Saw Dennis's answer, thought of a 2 byte essential improvement.
f(char*s){*s^=*s/3;}
Try it online! (Footer by Dennis.)
Like the original, modifies the first character of the string in place, but xors it with its value divided by 3 (the smallest number that works. 2 fails on the single character 'U' which gives 127, not printable.)
Braingolf, 12 bytes
.#~e1-:1+|&@
Subtracts 1 from the last char's ordinal if it is a tilde, otherwise adds one.
Explanation:
.#~e1-:1+|&@ Implicit input of string, convert each char to ordinal and push to stack
. Duplicate last item
#~ Push tilde ordinal
e If last 2 items are equal (consumes last 2 items)
1- ..then subtract one
: else
1+ ..add one
| endif
&@ Print entire stack as chars
Brachylog, 9 bytes
{¬Ṣ|∧Ịh}ᵐ
Explanation
This replaces all chars by a space, except spaces which it replaces with "0".
{ }ᵐ Map on each char of the Input
¬Ṣ The Input char is not " ", and the Output char is " "
| Or
∧Ịh The Output char is "0"
Alice, 9 bytes
#oi/
t i@
Explanation
The idea was taken from Martin Ender's CJam submission. The first character is taken as a code point, reduced mod 10, and moved to the end of the output. Since exactly one character was changed, permuting the characters cannot result in getting the same string back.
# skip next command
o (skipped)
i push first byte onto stack
STACK: [97]
/ reflect to SE, switch to ordinal mode (implicit reflect to SW)
i push remaining input onto stack as string (implicit reflect to NW)
STACK: [97, "sdf"]
o output top of stack (implicit reflect to SW)
STACK: [97]
t implicitly convert code point to decimal string, and extract last character
(implicit reflect to NE)
STACK: ["9", "7"]
o output this last digit (implicit reflect to SE)
i push empty string, since there is no more input to take (implicit reflect to NE)
/ reflect to S, switch to cardinal mode
@ terminate
Ruby, 20+1 = 21 bytes
Uses the -p flag.
sub(/./){$&=~/1/||1}
Replaces the first character in the input with a 0 if it is 1, or 1 otherwise.
Cubix, 10 bytes
..@|i?2%)O
Try it online! or Watch it run!
For each char, prints 1 if the char has an even code point, 2 otherwise; 1 has an odd code point and 2 an even, so the output will never equal the input.
Explanation
This code corresponds to the following cube net:
. .
@ |
i ? 2 % ) O . .
. . . . . . . .
. .
. .
The IP (instruction pointer) starts at the top-left corner of the far-left face, heading east. It follows this series of instructions:
i Grab a char-code from STDIN and push it to the stack (-1 if there is no more input).
? If the top item is negative, turn left and hit @ which ends the program.
If it's positive (any printable ASCII char), turn right. The IP runs through a bunch
of no-ops, then hits:
) Increment the top item.
| Mirror the IP's direction horizontally (turns it around).
) Increment the top item again. The IP then wraps around again until it hits:
? The top item is positive, so turn right.
2 Push a 2 to the stack.
% Push the modulus of the top two items (0 for even char-code, 1 for odd).
) Increment the result (now 1 for even char-code, 2 for odd).
O Output as a number. The IP wraps back around to the i and the process starts again.
JavaScript (ES6), 37 33 36 29 26 18 21 19 bytes
s=>s.slice(1)+ +!+s
-4 bytes thanks to ETHProductions
-7 + -5 + -2 bytes thanks to CalculatorFeline
-3 bytes thanks to Rick Hitchcock
Moves the first character to the end and sets it to 0 if it's numeric and non-zero, and 1 otherwise.
Explanation
s=> anonymous function with parameter s
+s convert s to a number
! not (converts to boolean; relevant: 0->true,1->false)
+ convert !+s back to number (true->1, false->0)
s.slice(1)+ prefix the rest of the string
␣ needed to avoid the +s combining
Proof
Because the second char becomes the first, the third char becomes the second, etc. all chars would have to be identical. The last remaining char can only be a 0 or a 1, so the repeated char would have to be either 0 or 1. But any string of 0s produces a 1 at the end, and vice-versa; therefore, it is impossible to create an input that is equal to its output. -ETHProductions
See edits for former versions and explanations.
f=
s=>s.slice(1)+ +!+s
console.log(f("000"))
console.log(f("111"))
console.log(f("001"))
console.log(f("110"))
console.log(f("~"))
console.log(f("111111111111111111111111111111111111111111111111111"))
console.log(f("Hello world!"))
console.log(f("23"))
console.log(f(" "))
console.log(f("1x"))
PHP, 30 27
<?=strtr($a=$argn,$a,$a^1);
Changes every each char equal to the first char with the char that has the least significant bit flipped.
Japt, 4 bytes
®c v
Explanation:
®c v
® At each char:
c Convert to its ASCII value
v Return 1 if divisible by 2, otherwise return 0
V, 7 bytes
íÁ/a
g?
Try it online! or Verify all test cases!
How does it work?
Consider all strings consisting of printable ASCII. Every string must either 1) Contain alphabetic characters, or 2) Contain no alphabetic characters.
So the way this program works is by first converting one non-alphabetic character into 'a', and then performing ROT13 on the input string.
í " Substitute:
Á " A non-alphabetic character ([^a-zA-Z])
/ " with
a " the letter 'a'
g? " Perform ROT13 on...
" (Implicit) the current line.
><>, 11 bytes
i:0(?;'0'=n
Explanation
Like my MATL answer, this transforms character '0' into '1' and all other characters into '0'.
i Input one char, or push -1 if there aren't any more chars
: Duplicate
0( Is it less than 0?
?; If so, end program. Else:
'0' Push character '0'
= Is it equal? Gives 1 or 0
n Output as a number. Go back to the beginning
JavaScript (ES6), 24 bytes
f=
s=>s.replace(/./,s[0]^1)
<input oninput=o.textContent=f(this.value)><pre id=o>
Changes the first character of the string according to this table:
1 0
2 3
3 2
4 5
5 4
6 7
7 6
8 9
9 8
Everything else becomes 1.
Aceto, 16 12 9 bytes
=01`
,'pO
Run with -F to see immediate output (but works without).
Explanation:
We read a character, then push the character 0 on the stack and test for equality:
=0
,'
If equal, we push a 1.
1`
We now print the top element (either a one or an implicit zero), and go back to the start:
pO
This prints zeroes for any characters that aren't zeroes, and ones for zeroes, e.g.:
Input | Output
----------------
abcde | 00000
11111 | 00000
10010 | 01101
Haskell, 19 bytes
An anonymous function which takes and returns a String. Use as (map$(!!1).show.succ) "1111".
map$(!!1).show.succ
Try it online! (Using @xnor's testing harness.)
- For each character in the input string, increments the character, then converts that to character literal format, then takes the second character of the literal, which is the character just after the starting
'quote. - For nearly all printable characters, this results in simply the incremented character. The exceptions are
&and~, which instead give\, because their successors'and\DELget escaped in character literals.
Jelly, 3 bytes
~Ṿṁ
Output is a string of digits, commas and hypen-minus characters, whose first character will differ from the first character of the input string.
How it works
~Ṿṁ Main link. Argument: s (string)
~ Map bitwise NOT over the characters c in s.
This attempts to cast c to int and then apply bitwise NOT, mapping
'0', ..., '9' to 0, ..., 9 (before ~), then -1, ..., -10 (after ~).
For non-digits, the attempt fails, mapping c to 0.
Ṿ Uneval, yielding a comma-separated string of integers in [-10, ..., 0].
The first character will be '-' if s starts with a digit and '0' if not.
ṁ Mold; truncate the result to the length of s.
Python 2, 21 bytes
lambda s:`s`[:len(s)]
Takes the string representation of the input string and truncates it to the length of the input string. For a typical string, this puts it in ' quotes and chops off the end:
abc -> 'abc' -> 'ab
rep chop
Note that the new string starts with '. Let's show that the output always differs from the input.
If the input has no
', then the output starts with'and the input does not.If the input contains a
'and but no", then Python will use"for the outer quotes, giving a first character"that's not in the input string.If the input has both
'and", then the outer quotes are'and each'is escaped as\'. Wherever the first"appears in the input, it's shifted right by the initial'in the output and by any possible escaping. This means it cannot match with a"in the corresponding position in the output.
Finally, note that quoting the input and possibly escaping characters always increases the number of characters, so truncating the output makes it the same length as the input.
Note that it was crucial that Python adaptively switches to " in the second case. If it didn't do so, it would fail on the three-character input '\'. Or, any longer prefix of the fix show string using '. So, this method won't work for most languages.
Bash + coreutils, 13
tr \ -~ ~\ -}
Transliterates the characters to ~ (0x20 - 0x7e) with ~, then to } (0x7e, 0x20 - 0x7d).
05AB1E, 3 bytes
ÇÈJ
Ç # Convert to ASCII values
È # is even? (0 is 48 and 1 is 49 therefore 0 -> 1 and 1 -> 0)
J # Join
Jelly, 4 bytes
^1Ṿ€
Outputs a digit string. No output character will be equal to the corresponding input character.
How it works
^1Ṿ€ Main link. Argument: s (string)
^1 XOR each character c in with 1.
This attempts to cast c to int, mapping '0', ..., '9' to 0, ..., 9.
For non-digits, the attempt fails, mapping c to 0.
After XORing with 1, we get 1, 0, 3, 2, 5, 4, 7, 6, 9, 8 for '0', ..., '9',
and 1 for all non-digits.
Ṿ€ Uneval each; mapping 0, ..., 9 to '0', ..., '9'. This yields a character
array, which is Jelly's string type.
Note that no character is mapped to itself.
Perl 6, 48 28 bytes
{(->{[~] (' '..'~').roll(.chars)}...*ne$_).tail}
Generates a sequence of random strings, stopping when one of them doesn't match the input. Returns the last string produced
{first *ne$_,(0,1 Xx.chars)}
Generates two strings one consisting of only 0s, the other of 1s. Returns the first that is not stringwise equal to (ne) the input ($_).
Expanded
{ # bare block lambda with implicit parameter 「$_」
first # return the first value where the following is true
* ne $_, # WhateverCode lambda ( 「*」 is the parameter )
(
0, 1 # list of 0 and 1
X[x] # cross 「X」 that using the string repetition operator 「x」
.chars # with the number of characters in 「$_」
)
}
MATL, 6 5 bytes
Qo!V!
Explanation
Q % Implicitly input a string. Add 1 to each code point.
o % Parity: 0 if odd, 1 if even. Note that '0' has ASCII code 48, so after
% having added 1 it now gives 1. Similarly. '1' has ASCII code 49, so it
% now gives 0. All other chars give 0 or 1.
!V! % Convert each number to the corresponding char. Implicitly display
Haskell, 20 bytes
map$head.show.(<'M')
Converts to a string of F and T. What matters is that the characters F and T converts to each other. This is done by checking if the character is less than M to get True or False, then taking the first character of the string representation.
Haskell, 23 bytes
q '~'=' '
q _='~'
map q
Replaces every character with ~, except ~ becomes a space.
Whitespace, 59 bytes
Visible representation
NSSNSSSTSSSSSNSNSSNSSNSTNTSTTTTSSTNTTSNSNSTSSSNSSSNTNSSNSNN
What it does:
For every character it reads it prints a space, except when it's a space, then it prints a @.
Disassembly:
loop:
push 32
dup
dup
dup
ichr
get
sub
jn not_32
dup
add
not_32:
pchr
jmp loop
Retina, 10 6 bytes
4 bytes golfed thanks to @Neil
T`p`~p
This transliterates to ~, ! to , " to !, ..., ~ to }.
CJam, 5 bytes
l)iA%
Converts the last character to its code point and takes that modulo 10. This is clearly different for non-digit characters in the last position. But the digits start at code point 48, so taking those mod 10 will shift them left cyclically and hence the last character is always changed.
Octave, 19 18 bytes
@(s)['',(s<66)+65]
Explanation:
@(s) % Anonymous function taking a string s as input
s<66 % A boolean vector with `1` for all characters below ASCII-66.
(s<66)+65 % Add 65 to this, so that all elements that are 32-65 are now 66.
% All other elements are 65
['', ] % Implicitly convert the list of 65 and 66 to the letters A and B
Brain-Flak, 53 bytes
Includes +1 for -c
([(((()()()()){}){}){}()]({}())){{}(<({}[()()])>)}{}
This will decrement the first character unless it is a space, in that case it will increment the first character.
([(((()()()()){}){}){}()] ) # Push: input + 1 != 33 on top of...
({}()) # input + 1
{{}(< >)}{} # If (input + 1 != 33)
({}[()()]) # Push: (input + 1) - 2
Haskell, 30 26 bytes
f ' '='~'
f c=pred c
map f
Replaces each char with its predecessor and space with tilde.
05AB1E, 5 bytes
žQDÀ‡
Explanation
Replaces each char with the next printable ascii char, wrapping from tilde to space.
žQ # push a string of printable acsii chars (space to tilde)
D # duplicate
À # rotate left
‡ # translate