| Bytes | Lang | Time | Link |
|---|---|---|---|
| 060 | JavaScript Node.js | 240826T014654Z | l4m2 |
| 020 | Japt | 240826T101247Z | Shaggy |
| 070 | Java | 240826T081310Z | Kevin Cr |
| 017 | 05AB1E legacy | 240826T074442Z | Kevin Cr |
| 019 | Charcoal | 240826T062329Z | Neil |
| 017 | Retina 0.8.2 | 240826T035406Z | Neil |
| 034 | K ngn/k | 240826T025549Z | akamayu |
| 021 | Jelly | 240826T022836Z | Jonathan |
JavaScript (Node.js), 60 bytes
x=>x.replace(/\W(\w*)/g,(_,y)=>y?(s+=`)(${y})`,'(*'):s,s='')
-1B from Shaggy
Japt, 20 bytes
r"%(%*"È+pUè,Ãr,"))(
r"%(%*"È+pUè,Ãr,"))( :Implicit input of string U
r :Replace
"%(%*" : Regex /\(\*/
È : Pass match through the following function
+p : Append itself repeated
Uè, : Count of "," in U times
à :End replace
r,"))( :Replace commas with parentheses
Java, 70 bytes
s->s.replace("(*","(*".repeat(s.split(",").length)).replace(",","))(")
Explanation:
s-> // Method with String as both parameter and return-type
s.replace("(*", // Replace the "(*"
"(*".repeat( // with itself repeated the following amount of times:
s.split(",") // Split the input-string on ","
.length)) // And get its length
.replace(",", // And also replace all ","
"))(") // with "))("
05AB1E (legacy), 19 17 bytes
',¢F„(*x.;',…))(:
-2 bytes ('(…(*(.; → „(*x.;) switching to the legacy version of 05AB1E (built in Python), where multiply on strings to repeat them works
Try it online or verify all test cases.
Explanation:
',¢ '# Count how many "," are in the (implicit) input-string
F # Pop and loop that many times:
# (use the implicit input again for the first iteration)
„(* # Push string "(*"
x # Double it to "(*(*" without popping
.; # Replace the first "(*" with "(*(*"
: # And then replace all
', '# ","
…))( # with "))("
# (after the loop, the result is output implicitly)
Charcoal, 19 bytes
≔⪪θ,η⪫⪪⪫η))(¦(*⭆η(*
Try it online! Link is to verbose version of code. Explanation: Port of @akamayu's K answer.
≔⪪θ,η
Split the input on ,. This saves a byte because otherwise extra separators would be needed between the literal strings.
⪫⪪⪫η))(¦(*⭆η(*
Join that split with ))(, then split again on (*, then join with (* repeated for each part of the original split.
Retina 0.8.2, 17 bytes
+`(\).+),
(*$1))(
Try it online! Link includes test cases. Explanation: Repeatedly replace each comma in turn from right to left with ))(, inserting an extra (* after the return type each time.
K (ngn/k), 34 bytes
Replace each , with ))( and repeat (* for number of , times.
{((2*#i)#"(*")/"(*"\"))("/i:","\x}
{((2*#i)#"(*")/"(*"\"))("/i:","\x}
","\ Split by ","
"))("/ Join by "))("
"(*"\ Split by "(*"
/ Join by
((2*#i)#"(*") "(*" repeated for 1 + number of "," times
Jelly, 21 bytes
iṂœṖjċ”,⁾(*ẋƊṣ”,j“))(
A full program that accepts a string and prints the result.
How?
iṂœṖjċ”,⁾(*ẋƊṣ”,j“))( - Main Link: list of characters, S
Ṃ - minimum {S} -> '('
i - first index of {'('}
œṖ - partition {S} before {that index}
Ɗ - last three links as a monad - f(S):
ċ”, - count occurrences of ','
⁾(* - "(*"
ẋ - repeat {"(*"} {count} times -> "(*(*(*...(*"
j - join {Partitioned S} with {"(*(*(*...(*"}
ṣ”, - split at occurrences of ','
j“))( - join with "))("
- implicit print