Übertragen Sie bei zwei Buchstabenfolgen das Großschreibungsmuster jeder Zeichenfolge auf die andere. Wenigste Bytes gewinnt.
Input: CodeGolf xxPPCGxx
Output: coDEGOlf XxppCgxx
- Beide Zeichenfolgen sind gleich lang und nicht leer, nur Buchstaben
a..z
undA..Z
. - Sie können die beiden resultierenden Zeichenfolgen in beliebiger Reihenfolge relativ zu den Eingaben ausgeben.
- Sie können ein Zeichenfolgenpaar als eine Zeichenfolge mit einem nicht aus Buchstaben bestehenden Trennzeichen für die Eingabe und / oder Ausgabe darstellen.
- Sie können eine Zeichenfolge als Liste von Zeichen oder Zeichenfolgen mit einem Zeichen darstellen, jedoch nicht als Folge von Codepunktwerten, es sei denn, es handelt sich lediglich um Zeichenfolgen in Ihrer Sprache.
- Ihre Eingabe und Ausgabe können Zeichenfolgen unterschiedlich darstellen.
Testfälle:
CodeGolf xxPPCGxx -> coDEGOlf XxppCgxx
lower UPPER -> LOWER upper
MiXeD lower -> mixed LoWeR
A A -> A A
ABcd EfGh -> AbCd EFgh
array[i++%n]+=...;
?array[t=i++%n]=array[t]+...;
funktioniert gut; undarray[i%n]+=...;i++;
funktioniert auch, aber die Verwendung voni++
oder++i
mit einem Modulo und+=
das Anhängen an eine Zeile in einem Array funktioniert nicht. Hier ein Java 10 TIO als Beispiel, um das Problem zu sehen. Handelt es sich um einen Fehler (oder eine Funktion: S) im Java 10 JDK oder im Java 10 TIO-Compiler?String
, zvar
.C (gcc) ,
86585553 BytesProbieren Sie es online!
quelle
f(S,Z)char*S,*Z;{for(int d;d=(*Z^*S)&32,*Z++^=d;)*S++^=d;}
c(a,s,e)char*a,*s;{for(;*s++^=e=(*s^*a)&32;)*a++^=e;}
(53 bytes)Gelee , 9 Bytes
Probieren Sie es online!
Wie es funktioniert
quelle
APL (Dyalog Classic) ,
1312 BytesProbieren Sie es online!
Ein- und Ausgabe ist eine 2 × N-Zeichenmatrix
⎕a
ist das englische Alphabet in Großbuchstaben'ABC...Z'
∊∘⎕a
returns a boolean matrix indicating which letters in the input are uppercase819⌶
converts its right argument to uppercase or lowercase depending on its boolean left argument ("819" is leetspeak for "BIG")819⌶¨⍨
does that for each (¨
) character, swapping (⍨
) the arguments⊖
means reverse vertically; one⊖
acts as the left argument to819⌶
and the other is the final actionquelle
"819" is leetspeak for "BIG"
... Seriously? That's the actual explanation for why it's 819? 0_oPyth, 10 bytes
Try it here!
Explanation & neat Pyth tricks used
rVV_mmrIk1
— Full program. Input is taken from STDIN as a list of two strings, and the output is written to STDOUT as a list of two lists of characters.mm
— For each character in each of the strings:Ik
— Check if it is invariant under...r...1
— ... Converting to uppercase. Yields True for uppercase characters and False for lowercase ones._
— Reverse that list.VV
— And double-vectorize the following function over the two lists:r
— Convert to uppercase if the value isTrue
(aka1
), else convert to lowercase.This submission abuses the fact that
r0
andr1
are the lowercase and uppercase functions in Pyth, and we use truth values (the values obtained by checking if each character is uppercase, reversed) yieldingTrue
for uppercase andFalse
for lowercase. The fact that booleans are subclasses of integers in Python is very handy for the approach this answer is using. Porting Dennis and Jonathan's Jelly approaches both resulted in more than 18 bytes, so I am quite happy with the Pyth-specific tricks used here.quelle
MATL, 11 bytes
Try it online! Or verify all test cases.
Explanation
quelle
Haskell, 78 bytes
Try it online!
quelle
isUpper x
can bex<'a'
.J,
36 3127 bytes-9 bytes thanks to FrownyFrog!
Try it online!
The previous solution was:
J,
3631 bytes-5 bytes thanks to FrownyFrog!
Try it online!
How it works:
quelle
[:
can be 0 and the(22 b.)
can beXOR
.&.(3&u:)
saves 1 byte."
and$
? Thanks!,:
, there are 2 rows on the left side. We need"(1)
but"$
works too, because it stands for"1 _
.$ b.0
gives the rank of $ (monadic, dyadic left, dyadic right).R,
118 94 7572 bytesTry it online!
There must be a much golfier way.-43 bytes thanks to Giuseppe who pointed me to the MATL solution by Luis Mendo. TIO link contains a function solution for the same byte count.Bonus: The output is a named vector whose names are the original input strings!
quelle
a<-
since you don't usea
anywhere else.x86-64 machine code, 14 bytes
Callable from C (x86-64 SysV calling convention) with this prototype:
An explicit-length version with length in
rcx
is the same size.void casexchg(char *rdi, char *rsi, int dummy, size_t len);
This uses the same bit-exchange algo as the C and Java answers: If both letters are the same case, neither needs to change. If they're opposite case, they both need to change.
Use XOR to diff the case bit of the two strings.
mask = (a XOR b) AND 0x20
is 0 for same or 0x20 for differing.a ^= mask; b ^= mask
caseflip both letters iff they were opposite case. (Because the ASCII letter codes for upper and lower differ only in bit 5.)NASM listing (from
nasm -felf64 -l/dev/stdout
). Usecut -b 26- <casexchg.lst >casexchg.lst
to turn this back into something you can assemble.The slow
loop
instruction is also 2 bytes, same as a shortjcc
.scasb
is still the best way to incrementrdi
with a one-byte instruction. I guess we couldxor al, [rdi]
/stosb
. That would be the same size but probably faster for theloop
case (memory src + store is cheaper than memory dst + reload). And would still set ZF appropriately for the implicit-length case!Try it online! with a _start that calls it on argv[1], argv[2] and uses sys_write on the result
quelle
k, 14 bytes
Try it online! Input/output is a list of two strings.
quelle
Python 3, 83 bytes
Try it online!
-3 bytes thanks to Mr. Xcoder
-3 bytes thanks to Chas Brown
quelle
Haskell, 78 bytes
Try it online!
quelle
QBasic, 133 bytes
Takes the two strings comma-separated and outputs the results newline-separated. Uses the bit-fiddling algorithm from Dennis's Jelly answer. Other than that, the main golf trick here is that the first result string is printed directly, one character at a time, which is a little shorter than saving both result strings in variables and printing them outside the loop.
quelle
JavaScript,
777473 bytesShow code snippet
Takes an array of char arrays, outputs an array of char arrays.
-1 byte (@Arnauld):
c>'Z'
→c>{}
quelle
c>{}
.Retina, 75 bytes
Try it online! Explanation: The newlines are used as markers to determine how much of the string has been processed. The regex tries to match against uppercase letters or failing that any characters. If an uppercase letter was matched then the other character is uppercased otherwise it is lowercased and vice versa, while the newlines are advanced to the next character.
quelle
Python 3,
7675 bytesTry it online!
Outputs the result as one string with a single-character separator.
Thx to Jonathon Allan for 1 byte.
quelle
(y>'Z')*32
->ord(y)&32
Assembly (nasm, x64, Linux), 25 bytes (123 bytes source)
Hex bytes:
The function entry point is at
a
, with the strings passed in usingRDI
andRSI
.Try it online!
quelle
and al,32
is only 2 bytes, using the special AL,imm8 encoding that most ALU instructions have. You could require the string length in RCX and useloop
. I was going to say you shouldtest ah,ah
because that's more efficient thanor
while being the same length, but it's longer in asm source so the crusty old idiom actually has merit for asm-source code golfing :PJelly, 13 bytes
Try it online!
Also 13:
=ŒuṚ×32ạŒlO$Ọ
(or=ŒuṚæ«5ạŒlO$Ọ
)quelle
Charcoal, 17 bytes
Try it online! Link is to verbose version of code. Takes input as an array of two strings. Explanation:
quelle
F#, 120 bytes
Bugger.
Try it online!
The function
g
takes the two strings as parameters.Seq.fold2
applies a function with an accumulator (a
) to each element (x
andy
) in the strings. Initiallya
is an empty string, and it adds the converted character to it in each iteration.b
is the main function. It first convertsf
with respect tos
, and then convertss
with respect tof
. It then returns a tuple with both values.quelle
Prolog (SWI), 121 bytes
Try it online!
quelle
Ruby,
7469 bytesTry it online!
Input and output are arrays of chars, so the footer does back and forth transformations from strings.
I'm not yet sure whether this is a good approach to the problem, but this challenge definitely looks like a nice use scenario for
swapcase
method.quelle
PHP 4.1.2, 40 bytes
Replace the pair of quotation marks with byte A0 (in ISO-8859-1 or Windows-1252, this is NBSP) to get the byte count shown, then run from a web browser (or from the command line), providing the strings as the query string arguments (or environment variables)
a
andb
.In this version of PHP, register_globals is on by default, so the strings will automatically be assigned to the variables
$a
and$b
. Increase the value2e5
(200000) if necessary.PHP 7.1+, 58 bytes
Run on the command line, using
php -r 'code here' string1 string2
:The value
3e5
(300000) is chosen to exceed (MAX_ARG_STRLEN * 2 + 1) on most Linux systems (specifically, x86 and other architectures for which PAGE_SIZE is 4096, and MAX_ARG_STRLEN is thus 131072), to avoid problems with any possible input string. Increase if necessary.Try it online!
quelle
Stax, 10 bytes
Run and debug it
Here's an ungolfed representation of the same program to show how it works.
Run this one
quelle
Crystal, 108 bytes
Try it online!
How it works?
quelle