# Create this “String Mix-Up” function in as few bytes as possible

This is my first time here at Code Golf SE so feel free to add your input/advice/suggestions to improve my post if necessary. The week after my computer science exam, the solutions were publicly published. One problem had a 45 line example solution but my solution was only 18 lines, (which I was very proud of), but I bet you guys here at code golf SE can do better.

Here are the requirements:

• Takes two inputs, string1 and string2.
• Delete the first occurrence of string2 found in the string1.
• Add string2 in reverse order to the beginning of string1, and
• Add string2 in reverse order to the end of the string1.
• If string2 does not occur in string1, do not modify string1.
• Output the modified string1

Example:

Input:

batman
bat

thebatman
bat

batman
cat


Output:

tabmantab

tabthemantab

batman


Note: The problem was intended to handle basic inputs. Assume all expected inputs are restricted to characters A-Z, numbers, or other special characters visible on your keyboard. It does not need to handle tabs or newlines.

Submissions with shortest byte counts in each language win.

• Can you give an example test case where string 2 doesn't occur in string 1? Also - which characters can appear in the string? Printable ASCII? Tab? Newlines? – user202729 Mar 6 '18 at 6:13
• @user202729 Input "batman, robin" has output "batman" – Ryan Mar 6 '18 at 6:17
• Out of curiosity, which language did you use in the exam? – user202729 Mar 6 '18 at 6:26
• And an example where string2 contains characters that are special in a regex and make naive s/string2// fail – Ton Hospel Mar 6 '18 at 7:19
• Perhaps a test case containing the second input multiple times, since we only should replace the first? Something like: "batman_and_his_bat", "bat""tabman_and_his_battab" – Kevin Cruijssen Mar 6 '18 at 9:48

# Stax, 16 11 bytes

ÄE┘¢8·╦Ωb•∩


Run and debug it online

The corresponding ascii representation of the same program is this.

Y#xyz|eyr|Sx?


This fixes the bugs in original version. It uses conditionals and stuff.

Y                   Save 2nd input in y.  Now the two inputs are x and y.
#                  Occurrences of y in x. (a)
xyz               x, y, ""
|e             replace first occurrence
yr|S         surround with y reversed. (b)
x        first input. (c)
?       a ? b : c


The result is printed implicitly.

# Python, 55 bytes

lambda s,t:s.replace(t,'',1).join(2*[t[::-1]*(t in s)])


Try it online!

Using join to get the a+b+a pattern. This lets us write expression for a only once while still using a lambda. Compare:

Python 2, 57 bytes

def f(s,t):r=t[::-1]*(t in s);print r+s.replace(t,'',1)+r


Try it online!

# APL (Dyalog Unicode), 36 bytesSBCS

Look ma, no regex!

Anonymous infix lambda, taking string2 as left argument and string1 as right argument. Assumes ⎕IO (Index Origin) to be 0, which is default on many systems.

{r,⍵[(⍳≢⍵)~(f⍳1)+⍳≢⍺],r←⌽⍺/⍨∨/f←⍺⍷⍵}


Try it online!

{} "dfn"; ⍺ is left argument, ⍵ is right argument; e.g. "bat" and "manbatmanbat":

⍺⍷⍵ find where string2 begins in string1; [0,0,0,1,0,0,0,0,0,1,0,0]

f← assign to f (find)

∨/ OR-reduction (does string2 occur at all?); 1

⍺/⍨ use that to compress string2 ("" if not found); "bat"

⌽ reverse that; "tab"

r← assign to r (reverse)

⍵[], prepend string2 indexed by the following indices:

≢⍵ the length of string2; 3

⍳ the indices of that; [0,1,2]

()+ add the following to that:

f⍳1 index of first 1 in f; 3

Now we have the indices to be removed; [3,4,5]

()~ the following indices except those indices:

≢⍵ length of string2; 12

⍳ the indices of that; [0,1,2,3,4,5,6,7,8,9,10,11]

Now we have the indices we want to keep; [0,1,2,6,7,8,9,10,11]

This gives us the remaining parts of string1 followed by a reversed string2; "manmanbattab"

r, prepend r to that; "tabmanmanbattab"

# C#, 148 bytes

using System.Linq;using System.Text.RegularExpressions;(a,b)=>a==(a=new Regex(Regex.Escape(b)).Replace(a,"",1))?a:(b=string.Concat(b.Reverse()))+a+b

• Perhaps include a tio link? (I edited your post to capitalize C# and add syntax highlighting.) – Οurous Mar 6 '18 at 8:39
• Welcome to PPCG, and nice first answer! With lambdas you are allowed to leave out the types and the trailing semi-colon, but all required imports are mandatory for the byte-count I'm afraid, so it would becomes this instead: using System.Linq;(a,b)=>a==(a=new System.Text.RegularExpressions.Regex(b).Replace(a,"",1))?a:(b=string.Concat(b.Reverse()))+a+b (128 bytes). Also, as @Ourous suggested, perhaps add a TIO link to your answer. Here is an example of how it might look like. It's unfortunate the Regex import is so long in .NET... :( – Kevin Cruijssen Mar 6 '18 at 9:30
• Hmm, it seems to fail for "this.is.a.test", ".". Expected output: ".thisis.a.test.", actual output: ".his.is.a.test.". Can be fixed like this: using System.Linq;using System.Text.RegularExpressions;(a,b)=>a==(a=new Regex(Regex.Escape(b)).Replace(a,"",1))?a:(b=string.Concat(b.Reverse()))+a+b (148 bytes). Try it online. – Kevin Cruijssen Mar 6 '18 at 11:00
• Thanks Kevin.. wow, c# is impressively unsuitable for golfing.. :) I tested my code in linqpad which I guess adds some commonly used namespaces by default. And yep, missed the need to escape it as someone pointed out in your java port of my code :) I think I am gonna use JavaScript next time. Thank you very much for taking the time. – lee Mar 6 '18 at 11:13

# Python 3, 56 bytes

lambda s,t:[s,t[::-1]+s.replace(t,'',1)+t[::-1]][t in s]


Try it online!

# Retina, 35 bytes

^(.*)¶(.*?)\1(.*)
¶$^$1$2$3$^$1
0A


# 05AB1E, 12 bytes

DIåiIõ.;IR.ø


Try it online!

Explanation

D             # duplicate 1st input
Iåi          # if 2nd input is in first input
Iõ.;      # replace the first occurrence of 2nd input with the empty string
IR.ø  # surround with 2nd input reversed

• Only other thing I can think of is 13-bytes: õ.;©Ê²si®¹R.ø but it's pretty different order-wise. – Magic Octopus Urn Mar 6 '18 at 15:25

# Javascript (ES6), 6965 64 bytes

x=>y=>x.match(y)?(k=[...y].reverse().join)+x.replace(y,'')+k:x


Try it online!

• Welcome to PPCG! / (1) In the way how we normally count byte count, the m= is not counted if you don't use it in the function (i.e., it doesn't call itself). (2) You can save a byte by currying. – user202729 Mar 6 '18 at 14:01
• Also you may find Tips for golfing in Javascript useful. – user202729 Mar 6 '18 at 14:01
• You can use [...y].reverse().join  to reverse a string, save 2 bytes. – user202729 Mar 6 '18 at 14:06
• Doesn't work when y contain |? – l4m2 Mar 7 '18 at 4:22

# K4, 36 bytes

Solution:

{$[#s:x ss y;r,(x_/(#y)#*s),r:|y;x]}  Examples: q)k){$[#s:x ss y;r,x_/(#y)#*s,r:|y;x]}["batman";"bat"]
"tabman"
q)k){$[#s:x ss y;r,(x_/(#y)#*s),r:|y;x]}["batman";"bat"] "tabmantab" q)k){$[#s:x ss y;r,(x_/(#y)#*s),r:|y;x]}["thebatman";"bat"]
"tabthemantab"
q)k){$[#s:x ss y;r,(x_/(#y)#*s),r:|y;x]}["batman";"cat"] "batman" q)k){$[#s:x ss y;r,(x_/(#y)#*s),r:|y;x]}["batbatbat";"bat"]
"tabbatbattab


Explanation:

If string 2 (y) is found in string 1 (x) then drop first indices of first occurrence.

{$[#s:x ss y;r,(x_/(#y)#*s),r:|y;x]} / the solution { } / lambda with implicit x,y$[         ;                   ; ]  / $[condition;true;false] x ss y / string-search for y in x s: / save result as s # / count length |y / reverse (|) y r: / save as r , / join with ( ) / do this together *s / first (*) s # / take (#y) / length of y x_/ / drop (_) over (/) r, / join with r x / (else) return x  # Jelly, 18 bytes œṣ⁹Ḣ;jɗ⁹ṭṚ};Ṛ}ðẇ@¡  Try it online! Full program. ## Javascript 85 bytes a=>b=>a.includes(b)?[...b].reverse().join+a.replace(b,'')+[...b].reverse().join:a  If someone knows how to avoid using split('').reverse().join('') for reversing string, please let me know f=a=>b=>a.includes(b)?[...b].reverse().join+a.replace(b,'')+[...b].reverse().join:a console.log(f('batman')('bat')) console.log(f('thebatman')('bat')) console.log(f('batman')('cat')) • for help, you can't get much better than the Tips for golfing in javascript, and I'm sure somebody will come along to help -- I don't know javascript. – Giuseppe Mar 6 '18 at 18:24 • There's really no better way to reverse a string. What you could do is ...?(c=[...b].reverse().join)+a.replace(b,'')+c:a to save quite a few bytes. – ETHproductions Mar 6 '18 at 21:15 # Javascript, ES6 - 73 bytes b=x=>y=>{v=[...y].reverse().join;u=x.replace(y,"");alert(u==x?x:v+u+v)}  • please support me to improve it! – NTCG Mar 6 '18 at 10:19 • Several of the JavaScript and ES6 tips will let you shave off a number of bytes. – Neil Mar 6 '18 at 11:03 • Also, we format code blocks with 4-spaces indentation. See this. – user202729 Mar 6 '18 at 11:31 • @user202729 thank you for reminding me! – NTCG Mar 7 '18 at 3:13 # Python 2, 62 Bytes def f(a,b):b*=b in a;a=a.replace(b,"",1);b=b[::-1];print b+a+b  # Jelly, 21 20 bytes œṣµḢ;j⁹$ɓ;;@ðU}
ç¹w?


Try it online!

Ouch! Why can other languages get 11~12 bytes? Am I doing something wrong?

And finally, I think I know how chain separators works...

œṣµḢ;j⁹$ɓ;;@ðU} Helper link (dyadic). General structure: œṣ <dyad> - given x and y, split x at sublists equal to y. Ḣ;j⁹$                <monad>             - given x, join with y except at
first index.
;;@                   <dyad>       - given x and y, return y+x+y.
Note that this link is flipped.
U}                      <dyad> - reverse right argument.

ç¹w?    Main link. Evaluate the last link if the second input is a substring
of the first input, otherwise - apply identity (do nothing).


# SNOBOL4 (CSNOBOL4), 73 bytes

	S =INPUT
T =INPUT
S T =	:F(O)
T =REVERSE(T)
S =T S T
O	OUTPUT =S
END


Try it online!

What's a regular expression?

# AWK, 85 bytes

{for(;x++<N=split($0,a,"");)p=a[x]p;$0=NR%2?L=$0:sub($0,"",L)?p L p:L}!(NR%2)


Try it online!

Input is: primary string on odd-lines, replacement on even lines. Replacement is case sensitive.

# Python 3, 58 bytes

def f(a,b):z=(b in a)*b[::-1];return z+a.replace(b,'',1)+z
`