| Bytes | Lang | Time | Link |
|---|---|---|---|
| 082 | Tcl | 250618T155401Z | sergiol |
| 065 | Haskell | 250618T072226Z | Ibozz91 |
| 074 | Commodore BASIC | 250617T203127Z | Mark Ree |
| 045 | Arturo | 230203T013424Z | chunes |
| 017 | Uiua SBCS | 240216T140549Z | chunes |
| 036 | Ruby | 221125T042330Z | Jordan |
| 072 | Janet | 250430T181119Z | Adam |
| 084 | Swift 6 | 250430T002821Z | macOSist |
| 050 | Juby | 221010T034622Z | Jordan |
| 072 | Scala | 230418T062829Z | 138 Aspe |
| 008 | Vyxal | 220507T003637Z | lyxal |
| 085 | Nibbles | 230325T223221Z | Adam |
| 123 | Rust | 230207T140430Z | JSorngar |
| 093 | Java 8 | 230203T175526Z | Fhuvi |
| nan | 230205T102636Z | The Thon | |
| 104 | Swift | 230203T160954Z | Bbrk24 |
| 078 | Python 3 | 221216T055200Z | user1139 |
| 010 | Japt | 221010T144247Z | Shaggy |
| 033 | Raku | 221010T191017Z | Sean |
| 010 | Pyth | 221010T181935Z | hakr14 |
| nan | 221010T141754Z | bigyihsu | |
| 047 | Python | 220509T152841Z | Samathin |
| 075 | R | 220509T101910Z | Dominic |
| 099 | SNOBOL4 CSNOBOL4 | 220510T161753Z | Giuseppe |
| 084 | Brainfuck | 220507T214125Z | Jiří |
| 010 | 05AB1E | 220508T145614Z | Command |
| 040 | Julia | 220509T091740Z | MarcMush |
| 044 | JavaScript Node.js | 220509T103014Z | ferikeem |
| 069 | JavaScript Node.js | 220507T225937Z | Noodle9 |
| 058 | PARI/GP | 220507T012816Z | alephalp |
| 020 | Burlesque | 220508T221636Z | DeathInc |
| 059 | C clang | 220507T005536Z | a stone |
| 028 | BQN | 220508T005012Z | Razetime |
| 021 | BQN | 220508T102201Z | ovs |
| 114 | Javascript Node.js | 220507T143931Z | LWS SWL |
| 053 | Factor | 220507T012416Z | chunes |
| 010 | Jelly | 220507T213051Z | Unrelate |
| 036 | Wolfram Language Mathematica | 220507T015811Z | att |
| 151 | C gcc | 220507T162407Z | LWS SWL |
| 021 | K ngn/k | 220507T151413Z | Traws |
| 012 | Husk | 220507T094840Z | Dominic |
| 083 | Bash | 220507T110956Z | Noodle9 |
| 010 | Charcoal | 220507T070415Z | Neil |
| 1513 | Retina | 220507T022517Z | m90 |
| 014 | Retina 0.8.2 | 220507T065437Z | Neil |
| 015 | Pip | 220507T050847Z | DLosc |
| 054 | Python | 220507T011119Z | loopy wa |
| 014 | Jelly | 220507T005320Z | hyperneu |
Tcl, 82 85 98 bytes
proc D x {join [lmap c [set X [split $x ""]] {join [linsert $X [incr i] '] ""}] +}
Commodore BASIC, 74 bytes
0INPUTE$:L=LEN(E$):FORI=1TOL:FORJ=1TOL:PRINTMID$(E$,J,1);:IFI=JTHENPRINT"'";
1NEXT:PRINT"+";:NEXT:PRINTCHR$(20)
This is a complete RUNnable program; the 74-byte figure is the amount of memory it takes up. The full-text source above is 111 characters, though it can be typed in using keyword abbreviations in only 83:
0inpute$:l=len(e$):fOi=1tol:fOj=1tol:?mI(e$,j,1);:ifi=jtH?"'";
1nE:?"+";:nE:?cH(20)
Uiua SBCS, 21 20 17 bytes
/⊂₃@+≡⌟⍜⊙↻⊂@'+₁°⊏
-1 thanks to janMakoso
-3 thanks to language updates and janMakoso
Ruby, 36 bytes
->s{s.chars.map{s.sub _1,'\&\''}*?+}
Ruby, 36 bytes
Alternate based on Deadcode's comment on OP.
->s{s.gsub(/./){"+#$`#$&'#$'"}[1..]}
Janet, 72 bytes
|(string/join(seq[n :keys $](string(take(inc n)$)"'"(drop(inc n)$)))"+")
Swift 6, 84 bytes
let d={s in{(1...(s+"").count).map{s.prefix($0)+"'"+s.dropFirst($0)}.joined}()("+")}
Scala, 72 bytes
Golfed version. Try it online!
def f(l:String)=l.indices.map(i=>l.patch(i,s"${l(i)}'",1)).mkString("+")
Ungolfed version
object Main extends App {
def f(l: String): String = {
l.indices.map(i => l.patch(i, s"${l(i)}'", 1)).mkString("+")
}
val test = List("abc", "de", "longabcdef", "short")
val expected = List(
"a'bc+ab'c+abc'",
"d'e+de'",
"l'ongabcdef+lo'ngabcdef+lon'gabcdef+long'abcdef+longa'bcdef+longab'cdef+longabc'def+longabcd'ef+longabcde'f+longabcdef'",
"s'hort+sh'ort+sho'rt+shor't+short'"
)
val checker = List("✖️", "✓")
for ((t, e) <- test zip expected) {
val s = f(t)
val checkMark = checker(if (s == e) 1 else 0)
println(s"$t -> $s $checkMark")
}
}
Vyxal, 8 bytes
ż\'vṀ\+j
No clue why I didn't vectorise insertion back in May 2022.
Explained
żƛ?n\'Ṁ;\+j
żƛ # For each item P in the range 1...len(input):
?n # at position P in the input,
\'Ṁ # insert a '
; # end map
\+j # join the result of that on "+" and output
Nibbles, 8.5 bytes
*"+".,,@::`<$_"'"
*"+".,,@::`<$_"'"
* Join
"+" with plus signs
. for each n in
, range
, length
@ input
: join
: join
`<$ first n characters of
_ input
"'" apostrophe
the remaining characters
Rust, 123 bytes
|s|{s.chars().map(|_|s).enumerate().map(|(i,w)|{let mut t=w.repeat(1);t.insert(i+1,'\'');t}).collect::<Vec<_>>().join("+")}
Java 8, 93 bytes
s->s.chars().mapToObj(e->s.replace((char)e+"",(char)e+"'")).reduce("",(a,b)->a==""?b:a+"+"+b)
Another close solution not using Streams (95 bytes) :
s->{String r="";for(String c:s.split("")){r+=(s+"+").replaceAll(c+"\\+{0,1}",c+"'");}return r;}
Thunno, \$ 16 \log_{256}(96) \approx \$ 13.17 bytes
LDIA*'+sjsAp''sj
Port of DLosc's Pip answer.
Explanation
LDIA*'+sjsAp''sj # Implicit input
LD # Length of input and duplicate
IA* # Wrap input in a list and repeat length times
'+sj # Join this list by "+"
sAp # Split into chunks of length of input
''sj # Join this list by "'"
# Implicit output
Swift, 104 bytes
Swift is not a good language for string manipulation.
{var a=""
for i in $0.indices{var c=$0
c.insert("'",at:c.index(after:i))
a+=c+"+"}
a.popLast()
return a}
Type is (String) -> String or (Substring) -> String.
Swift, 120 bytes
Not as short, but I think the way that a is initialized is funny.
{var a=[$0][..<0]
for i in $0.indices{var c=$0
c.insert("'",at:c.index(after:i))
a+=[c]}
return a.joined(separator:"+")}
If Array is a pair (pointer, length)*, indexing operations are like this:
arr[i]=*(pointer + i)arr[i...]=(pointer + i, length - i)arr[..<i]=(pointer, i)arr[...i]=(pointer, i + 1)arr[i..<j]=(pointer + i, j - i)arr[i...j]=(pointer + i, j - i + 1)arr[...]=(pointer, length)**
So, arr[..<0] creates an empty collection no matter what the array is. Here, I'm applying it to the array literal [$0], which you would never do in practice but is technically legal.
*It's actually a triplet (pointer, length, capacity), but this gets the point across.
**Yes, this exists for some reason.
Python 3, 78 bytes
d=lambda s:'+'.join([s[:i+1]+'\''+s[i+1:] for i in range(len(s)) if i<len(s)])
Japt, 10 bytes
¬£i''YÄÃq+
¬£i''YÄÃq+ :Implicit input of string
¬ :Split
£ :Map each element at 0-based index Y
i'' : Insert a "'" in U at index
YÄ : Y+1
à :End map
q+ :Join with "+"
Raku, 33 bytes
This 33-byte version works on my local recent-ish version of Raku:
{join '+',.comb.map:{S/$^a/$a'/}}
But not on TIO, where a few more bytes are needed:
{join '+',.comb.map:{$^a;S/$a/$a'/}}
.combbreaks the input string up into characters..map: { S/$^a/$a'/ }returns a copy of the input string for each character, in which that character has had an apostrophe appended to it.join '+'joins those strings together with a plus sign.
Pyth, 10 bytes
j\+XRQ\'Sl
Explanation:
j\+XRQ\'Sl | Full code
j\+XRQ\'SlQ | with implicit variables
------------+--------------------------------------
R SlQ | For each 1-index d of the input,
X Q\' | Insert "'" into the input at index d
j\+ | Join with "+"
Go, 150 bytes
import."strings"
type S=string
func f(s S)S{b:=[]S{}
for i:=range s{k:=""
for j,d:=range s{k+=S(d)
if j==i{k+="'"}}
b=append(b,k)}
return Join(b,"+")}
Using strings.Builder, 153 bytes
import."strings"
func f(s string)string{b:=Builder{}
w:=b.WriteRune
for i:=range s{w('+')
for j,d:=range s{w(d)
if j==i{w('\'')}}}
return b.String()[1:]}
Surprised how the string builder solution is only 3 bytes logner.
Python, 47 bytes
lambda s:"+".join(s.replace(c,c+"'")for c in s)
How it works:
a b c d --> a' b c d
a b c d --> a b' c d
a b c d --> a b c' d
a b c d --> a b c d'
then join with '+'
R, 81 75 bytes
Edit: -6 bytes thanks to Giuseppe
function(x,`[`=substring)paste0(x[1,s<-1:nchar(x)],"'",x[s+1],collapse="+")
Takes advantage of the vectorization of substring over a vector of start (& optional end) positions, and the vectorization & argument-recycling of paste over vectors of strings to paste-together.
SNOBOL4 (CSNOBOL4), 99 bytes
I =INPUT
N X =X + 1
I TAB(X) . L REM . R :F(O)
O =O "+" L "'" R :(N)
O O TAB(1) REM . OUTPUT
END
Brainfuck, 87 84 bytes
>,>---[>+<+++++++]>++[>,]<[<]<[<]>[[.>]>.>[.[-<<+>>]>[.>]<[<]<++++.----[->+<]<[<]]>]
Saved 3 bytes thanks to Nitrodon.
Explanation:
memory progress for abc:
null a null 'bc
null ab null 'c
null abc' null
string is split to two parts by null character
>,> load first character
---[>+<+++++++]>++ load ' to memory; taken form esolangs wiki
[>,] load rest of input
<[<]<[<]> go to beginning of memory
[
[.>] print first part
>.> print '
[ check if there is text after '
. print first char of second part
[-<<+>>] move char to the first part
>[.>] print rest of second part
<[<]< go to position of '
++++.---- use ' to print plus
[->+<] move '
<[<] go before beginning
]
> move back to beginning
if there wasn't text after ' move after end instead
]
05AB1E, 10 bytes
ε''«Nǝ}'+ý
ε map each character (of the implicit input string)
'' the ' character
« append to the current character
N iteration index
ǝ replace the input string's Nth character with the character with ' appended
}
'+ + character
ý join by it
Julia, 40 bytes
a->join([replace(a,i=>"$i'") for i=a],+)
only works with all letters distinct
Why...how...what...
[... for i=a]iterates over the stringaand store the results in a list.iwill be aCharreplace(a, i=>"$i'")replaces all characters equal toiinawithifollowed by'join(..., +)joins the list with+.+is a function, sostring(+) == "+"is used.
JavaScript (Node.js), 46 44 bytes
l=>[...l].map(s=>l.replace(s,s+"'")).join`+`
-2 bytes thanks to emanresu A
JavaScript (Node.js), 83 \$\cdots\$ 72 69 bytes
s=>g=(r="",i=0)=>s[i]?g(r+("+"+s).slice(!i,++i+1)+"'"+s.slice(i),i):r
Saved 7 8 bytes thanks to emanresu A!!!
Saved 3 bytes thanks to Samathingamajig!!!
PARI/GP, 58 bytes
s->j=strjoin;j([j(strsplit(s,c),Str(c"'"))|c<-Vec(s)],"+")
A port of @att's Mathematica answer.
Takes input as a string. Requires that all characters are unique.
PARI/GP, 64 bytes
v->i=1;j=strjoin;j([j([j(v[1..i]),j(v[i++..#v])],"'")|c<-v],"+")
Take input as a list of characters.
Burlesque, 20 bytes
JiTjiS{''IC}Z[[-'+IC
J # Dup
iT # Tails
j # Swap
iS # Heads
{
''IC # Intercalate "'"
}Z[ # Zip together then map
[- # Drop head
'+IC # Intercalate "+"
C (clang), 69 64 59 bytes
-10 thanks to @Noodle9 and @ceilingcat
l;f(char*a){for(l=0;a[l];)printf("+%.*s'%s"+!l,++l,a,a+l);}
BQN, 28 bytesSBCS
{1↓⥊'''∾˘⟨∘,≠𝕩⟩⥊«⥊'+'∾˘⊢⌜˜𝕩}
returns the required string with a trailing space. removing that is +3 bytes
-3 from ovs, who also made this tacit.
BQN, 21 bytesSBCS
1↓⟜∾'+'∾¨1↓↑∾⟜'''⊸∾¨↓
↑ gives prefixes, ↓ suffixes. ∾⟜'''⊸∾¨ joins matching prefixes and suffixes with a single quote.
1↓ removes the first value (empty prefix ∾ ' ∾ full suffix).
'+'∾¨ prepends a + to each string.
1↓⟜∾ joins the string and removes the leading +.
Javascript (Node.js), 114 bytes
function a(b){l=b.length;s="";for(i=1;i<=l;i++){if(i>1)s+='+';s+=b.substring(0,i)+"'"+b.substring(i,l);}return s;}
I forgot to add "+"
+10 characters to remove a trailing '+'
Factor, 53 bytes
[ dup length [1,b] [ cut "'"glue ] with map "+"join ]
If the order of each product doesn't matter:
Factor, 43 bytes
[ all-rotations [ 39 suffix ] map "+"join ]
Explanation
! "short"
all-rotations ! { "short" "horts" "ortsh" "rtsho" "tshor" }
[ 39 suffix ] map ! { "short'" "horts'" "ortsh'" "rtsho'" "tshor'" }
"+"join ! "short'+horts'+ortsh'+rtsho'+tshor'"
Jelly, 10 bytes
ṛ€j”+sLj”'
Port of DLosc's Pip solution.
€ For each element of the input,
ṛ the input.
j”+ Join the copies on +,
sL split into slices the length of the original input,
j”' and join those on '.
Jelly, 11 bytes
p”'$ṛ¦ⱮJj”+
The implementation of ¦ is a bit questionable, so we can just abuse Cartesian product to make up for it.
p”' Pair each element of the input with ',
$ṛ¦ and substitute elements of that into the input at
ⱮJ each index, individually.
j”+ Join the results on +.
Just for fun, without preserving the order within each term, ṭ-Ƥ”'ż@j”+ is 10.
Wolfram Language (Mathematica), 45 36 bytes
aRiffle[a/.#->#<>"'"&/@a,"+"]<>""
Input [{characters...}]. Assumes all characters in input are unique. Returns a string.
Wolfram Language (Mathematica), 35 bytes
ToString@Expand[Dt[1##]/._@a_:>a']&
Input [symbols...]. Assumes symbols are unique and not defined. Returns a spaced string.
Dt treats strings as constants.
C (gcc), 151 bytes
char*f(char*c,int i){char*r=malloc(i*i+i);char*o=r,k=0;for(;k<i*i;){if(k++%(i+1)==1)*o++='\'';*o++=*c++;if(!*c)c-=i,*o++='+';}*o--=0;*o='\'';return r;}
K (ngn/k), 21 bytes
Most straightforward K solution.
{"+"/?[x;;"'"]'1+!#x}
Explanation
{"+"/?[x;;"'"]'1+!#x}
?[x;;"'"] / insert "'" in string at
' / each position
1+!#x / 1..len of string
"+"/ / join the resulting strings with "+"
Bash, 83 bytes
v=$1\';until [ $v = \'$1 ];do a+=+$v;v=$(sed "s/\(.\)'/'\1/"<<<$v);done;echo ${a#+}
Charcoal, 10 bytes
⪫⪪⪫Eθθ+Lθ'
Try it online! Link is to verbose version of code. Explanation: Port of @DLosc's Pip answer.
θ Input string
E For each character
θ Input string
⪫ Join on
+ Literal string `+`
⪪ Split into substrings of length
θ Input string
L Length
⪫ Join on
' Literal string `'`
Implicitly print
Retina, 15 13 bytes
L$|+`.
$>`'$'
-2 thanks to Neil.
Match every character. For each one, take the text before the separator after the matched string, followed by an apostrophe, followed by the text after the matched string. Print as a list with separator +.
Retina 0.8.2, 14 bytes
.
$`$&'$'+
.$
Try it online! Link includes test cases. Explanation:
.
$`$&'$'+
Replace each variable with its derivative multiplied by the other terms plus a trailing + to sum the products together.
.$
Delete the final trailing +.
Pip, 15 bytes
aRL#aJ'+<>#aJ''
Explanation
aRL#aJ'+<>#aJ''
a Command-line arg "abc"
#a Length of arg 3
RL Repeat-list ["abc"; "abc"; "abc"]
J'+ Join on "+" "abc+abc+abc"
<>#a Groups of size len(a) ["abc"; "+ab"; "c+a"; "bc"]
J'' Join on "'" "abc'+ab'c+a'bc"
Python, 54 bytes
lambda s:((l:=len(s))*(l*"'"+-~l*(s+"+")))[-~l*~l::-l]
Python, 56 bytes
lambda s:((l:=-~len(s))*(l*(s+"+")+-~l*"'"))[l:-3*l:-~l]
Old Python, 58 bytes
lambda s:((l:=-~len(s))*(l*(s+"+")+l*"'"+"+"))[l:-3*l:-~l]
How?
a b c d +
(a)b c d +
a(b)c d +
a b(c)d +
a b c(d)+
' ' ' '(')'
a b c d(+)
a b c d +
(a)b c d +
a(b)c d +
a b(c)d +
' ' '(')' '
a b c(d)+
a b c d(+)
a b c d +
(a)b c d +
a(b)c d +
' '(')' ' '
a b(c)d +
etc.
Jelly, 14 bytes
J‘œṖ€j€”'j”+”'
J‘œṖ€j€”'j”+”' Full Program
J [1, 2, ..., len]
‘ [2, 3, ..., len + 1]
œṖ€ For each of these, partition (*)
j€”' Join each partition on ' (**)
j”+ Join on +
”' An unparseable nilad forces the current string to be printed
(And then the ' is printed at the end)
(*) partition takes a list on the left but since we give it a single number,
it auto-wraps it so we just get all needed splits
(**) the last partition only has one sublist so join doesn't work properly here