83
\$\begingroup\$

To "function nest" a string, you must:

  • Treat the first character as a function, and the following characters as the arguments to that function. For example, if the input string was Hello, then the first step would be:

    H(ello)
    
  • Then, repeat this same step for every substring. So we get:

    H(ello)
    H(e(llo))
    H(e(l(lo)))
    H(e(l(l(o))))
    

Your task is to write a program or function that "function nests" a string. For example, if the input string was Hello world!, then you should output:

H(e(l(l(o( (w(o(r(l(d(!)))))))))))

The input will only ever contain printable ASCII, and you may take the input and the output in any reasonable format. For example, STDIN/STDOUT, function arguments and return value, reading and writing to a file, etc.

For simplicity's sake, you may also assume the input will not contain parentheses, and will not be empty.

Input:
Nest a string
Output:
N(e(s(t( (a( (s(t(r(i(n(g))))))))))))

Input:
foobar
Output:
f(o(o(b(a(r)))))

Input:
1234567890
Output:
1(2(3(4(5(6(7(8(9(0)))))))))

Input:
code-golf
Output:
c(o(d(e(-(g(o(l(f))))))))

Input:
a
Output:
a

Input:
42
Output:
4(2)

As usual, all of our default rules and loopholes apply, and the shortest answer scored in bytes wins!

\$\endgroup\$
  • 21
    \$\begingroup\$ Ahem: Is this message anything to do with the challenge? :-) \$\endgroup\$ – wizzwizz4 Oct 18 '16 at 18:46
  • 12
    \$\begingroup\$ T​I​L 4​2​ ​= 8 \$\endgroup\$ – ETHproductions Oct 18 '16 at 21:48
  • \$\begingroup\$ What is maximum length for the input string? Incase of recursive methods \$\endgroup\$ – Ferrybig Oct 23 '16 at 20:30
  • 1
    \$\begingroup\$ @kamoroso94 You may take the input and the output in any reasonable format. A list of characters seems perfectly reasonable to me. \$\endgroup\$ – DJMcMayhem Sep 1 '17 at 15:29
  • 3
    \$\begingroup\$ So that's what Lisp code looks like \$\endgroup\$ – caird coinheringaahing Dec 17 '17 at 18:35

100 Answers 100

1
\$\begingroup\$

Gema, 31 13 characters

Shamelessly borrowing Titus's idea from his recursive PHP solution.

\B?=?
?#=(?#)

Sample run:

bash-4.3$ echo -n 'Hello world!' | gema '\B?=?;?#=(?#)'
H(e(l(l(o( (w(o(r(l(d(!)))))))))))
\$\endgroup\$
1
\$\begingroup\$

Scala, 46 bytes

(s:String)=>(s.init:\(""+s.last))(_+"("+_+")")

Explanation:

(s:String)=>    //define a function
  (s.init       //take everything but the last char
   :\           //foldRight
   (""+s.last)    //with the last char as a string as a start
  )(              //combine the chars right to left with this function:
    _+"("+_+")"   //take the char, append "(", append everything we've got so far, append ")"
  )
\$\endgroup\$
1
\$\begingroup\$

Groovy, 38 36 Bytes thanks to @manatwork

{it.reverse().inject{r,i->i+"($r)"}}

Yeah, the C&P messed up the first time.

\$\endgroup\$
  • \$\begingroup\$ I think your code was destroyed on copy paste, as is neither functional and is far from your byte count. BTW, no need for braces around simple variables in expansion. \$\endgroup\$ – manatwork Oct 19 '16 at 19:14
  • \$\begingroup\$ @manatwork was missing a brace, the braces are for string interpolation and you're right I effed the byte-count, forgot it needed reverse. \$\endgroup\$ – Magic Octopus Urn Oct 19 '16 at 19:41
  • \$\begingroup\$ I use lettercount.com for groovy byte count, NO IDEA what was going on. I thought 68 seemed high... \$\endgroup\$ – Magic Octopus Urn Oct 19 '16 at 19:42
  • 1
    \$\begingroup\$ Cool. But you still have extra braces in interpolation: "(${r})""($r)". At least Groovy 2.4.5 is happy without them too. \$\endgroup\$ – manatwork Oct 19 '16 at 19:51
  • \$\begingroup\$ @manatwork most of my experience is with Groovy on Grails and $ notation isn't accepted in views AFAIK. \$\endgroup\$ – Magic Octopus Urn Oct 19 '16 at 19:52
1
\$\begingroup\$

Bash / sed, 74 bytes

y=$(echo $1|sed "s/./\0(/g");z=$(echo $1|sed "s/./)/g");echo ${y%?}${z%?}
  • Puts a parenthesis after each characters in y.
  • Puts a parenthesis for each characters in z.
  • Print x and z truncated of one character.

To test, put this code into a file, and run the shell script with any arguments.

\$\endgroup\$
1
\$\begingroup\$

Python 2, 60 57 51 Bytes

def c(s):if(len(s)==1):return s;return s[0]+"("+c(s[1:])+")"

After some clarification on the rules from @manatwork on white space,

def c(s):return s if(len(s)==1)else s[0]+"("+c(s[1:])+")"

Thanks again @manatwork

def c(s):return s[0]+"("+c(s[1:])+")"if s[1:]else s

Ungolfed:

def c(s):
    if(len(s)==1):
        return s;
    return s[0]+"("+c(s[1:])+")"

Recursively calls itself and adds to the string.

c("CodeGolf") = 'C(o(d(e(G(o(l(f)))))))'
\$\endgroup\$
  • 3
    \$\begingroup\$ This doesn't work. You can't have if statements in one line. SyntaxError \$\endgroup\$ – Blue Oct 19 '16 at 19:00
  • \$\begingroup\$ I just put in on the one line to show it concisely without whitespace. You actually run the ungolfed version \$\endgroup\$ – bioweasel Oct 19 '16 at 19:23
  • 1
    \$\begingroup\$ Sorry, @bioweasel, but the spaces needed for the code to run have to be counted. Better refactor it to have a single statement inside the function: def c(s):return s if(len(s)==1)else s[0]+"("+c(s[1:])+")". \$\endgroup\$ – manatwork Oct 19 '16 at 20:02
  • \$\begingroup\$ Ah, gotcha. Sorry, this is my first time and I wasn't quite sure on the rules 100%, especially with Python and the whitespace \$\endgroup\$ – bioweasel Oct 19 '16 at 20:09
  • \$\begingroup\$ You could use that s[1:] syntax in the condition too: def c(s):return s[0]+"("+c(s[1:])+")"if s[1:]else s. \$\endgroup\$ – manatwork Oct 19 '16 at 20:24
1
\$\begingroup\$

Dart 44 bytes

p(s)=>s.split("").join("(")+")"*~-s.length;

I tried to be clever, but nothing beat this simple version. Notable mention:

r(s,[x=0])=>s[x++]+(x<s.length?"(${r(s,x)})":"");
q(s)=>s[0]+((s=s.substring(1))==""?s:"(${q(s)})");

but they drowned in necessary parentheses.

\$\endgroup\$
1
\$\begingroup\$

Java, 72 bytes

(a,s,l)->{l=a.length;s=""+a[--l];for(;l>0;)s=a[--l]+"("+s+")";return s;}

Ungolfed

public class Main {

  interface X {
    String f(char[]a,String s,int l);
  }

  static X x = (a,s,l) -> {
    l = a.length;
    s = "" + a[--l]; // start with the last character
    for (;l>0;)
      s = a[--l] + "(" + s +")"; // wrap in parentheses and prepend with the previous letter.
    return s;
  };

  public static void main(String[] args) {
    System.out.println(x.f("Hello World!".toCharArray(),"",0));
  }
}
\$\endgroup\$
1
\$\begingroup\$

C, 84 82 76 70 68 66 Bytes

i;f(char*s){for(i=1;*s+~i;putchar(*s?i&1?*s++:40:41),i+=*s?1:-2);}

Now using just one for loop and one putchar...

Test is like this

main(c,v)char**v;
{
    f(v[1]);puts("");
    f("foobar");puts("");     
    f("code-golf");puts("");     
}

output

H(e(l(l(o( (W(o(r(l(d))))))))))
f(o(o(b(a(r)))))
c(o(d(e(-(g(o(l(f))))))))
\$\endgroup\$
1
\$\begingroup\$

JavaScript, 36 bytes

([...v])=>v.join`(`+v.fill``.join`)`

I can't seem to beat the top score of 34 bytes, but I thought I would share my different approach.

\$\endgroup\$
1
\$\begingroup\$

Clojure, 94 60 59 48 bytes

-34 by making it a actual recursive solution. The biggest saving here was getting rid of the repeat part to generate the end brackets.

-1 by rearranging it, eliminating a conditional.

-11 bytes thanks to NikoNyrh. Now deconstructs the parameter directly.

(defn n[[f & r]](if(str f(if r(str\((n r)\))))))

Recursive. Basically (str head "(" (recur tail) ")"), with the brackets being added only if a tail exists.

Uses unoptimized recursion. Can handle strings up to around 5235 characters long.

Ungolfed:

(defn nest [[f & r]]
    (if f ; When it exists, construct string and recur, else, base-case
      (str f (if r (str \( (nest r) \))))))
\$\endgroup\$
  • \$\begingroup\$ I guess you could merge function definition and let to (defn n[[f & r]](if f(str f(if r(str\((n r)\))))) :) \$\endgroup\$ – NikoNyrh Jan 11 '17 at 0:38
  • \$\begingroup\$ @NikoNyrh You're right! I hate deconstructing in the parameter list in real code, but that certainly helps here. Thanks! \$\endgroup\$ – Carcigenicate Jan 11 '17 at 0:39
1
\$\begingroup\$

Python 3, 43 bytes

lambda x:print(*x,sep='(',end=')'*~-len(x))

Longer than shortest Python submission, but it is fairly different, so I thought I should post it. It is a function, but prints to STDOUT.

\$\endgroup\$
  • \$\begingroup\$ This prints one extra close paren each time. Try ~-len(x) rather than len(x) \$\endgroup\$ – Post Rock Garf Hunter Jan 11 '17 at 2:16
  • \$\begingroup\$ @WheatWizard Thank you, I don't know how I missed that. \$\endgroup\$ – nedla2004 Jan 11 '17 at 12:49
1
\$\begingroup\$

Haskell, 38 30 bytes

8 bytes saved thanks to @JanDvorak

f[x]=[x]
f(x:s)=x:'(':f s++")"

This is my first attempt at a haskell golf, probably not optimal yet.

Explanation

We define a function f. If this function receives input that matches the pattern [x], that is a length 1 string, we return the input. If we receive anything else as input we return the x:'(':f s++")", or the first character plus the rest result of f on the rest of the string all enclosed in parentheses.

\$\endgroup\$
1
\$\begingroup\$

Groovy, 46 bytes

{it.reverse().inject{r,i->")$r($i"}.reverse()}
\$\endgroup\$
1
\$\begingroup\$

Swift 3.1,  85  83 bytes

This is an anonymous function accepting an Array of Strings and returning a String.

{$0.joined(separator:"(")+String(repeating:")",count:$0.count-1)}as([String])->String

Try it here!

Swift 3.1,  88 87  86 bytes

This is a named function accepting an Array of Strings and printing a String.

func f(n:[String]){print(n.joined(separator:"(")+(1..<n.count).map{_ in")"}.joined())}

Try it here!

\$\endgroup\$
1
\$\begingroup\$

Ly, 50 bytes

iyspr1[1[=!["("o1$]p1$]1[=[pp2$]p1$]o]l1-[")"o1-];

Try it online!

This is ridiculous. Ly is lacking severely in the string manipulation department.

\$\endgroup\$
1
\$\begingroup\$

Java 8 (63 82 bytes)

"s" is a char[]

s->{String r="",p=r;for(char c:s){r+="("+c;p+=")";}return r+p;}

s->{String r="",p=r;for(char c:s){if(r==p)r+=c;else{r+="("+c;p+=")";}}return r+p;}

Edit: I didn't realise there aren't parentheses around the whole string.

\$\endgroup\$
1
\$\begingroup\$

Implicit, 24 23 bytes

~@~.(0-1@40@~.);;(0@41;

Try it online!

Ungolfed/explained:

~@~.     # read first character, print, read second, increment
(0       # do-while top of stack truthy
 -1      #  decrement top of stack
 @40     #  print open parenthesis (ASCII 40)
 @       #  print top of stack
 ~.      #  read char and increment
)        # while top of stack truthy
;;       # pop top two values (EOF, last char)

(0       # do-while top of stack truthy
 @41     #  print close parenthesis (ASCII 41)
 ;       #  pop top of stack
¶        # (implicit) forever

Test cases:

Nest a string
N(e(s(t( (a( (s(t(r(i(n(g))))))))))))

foobar
f(o(o(b(a(r)))))

1234567890
1(2(3(4(5(6(7(8(9(0)))))))))

code-golf
c(o(d(e(-(g(o(l(f))))))))

a
a

42
4(2)
\$\endgroup\$
1
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 111 bytes

	INPUT LEN(1) . O REM . N
	W =SIZE(N)
N	N LEN(1) . X REM . N	:F(O)
	O =O '(' X	:(N)
O	OUTPUT =O DUPL(')',W)
END

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Canvas, 8 bytes

(*;L╷)×+

Try it here!

\$\endgroup\$
1
\$\begingroup\$

Kotlin, 83 63 bytes

-20 bytes using toList; thanks to 12Me21 tipping me off this could be shorter.

{s:String->s.toList().joinToString("(")+")".repeat(s.length-1)}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Whitespace, 132 bytes

[S S S N
_Push_0][N
S S N
_Create_Label_LOOP][S N
S _Duplicate][S N
S _Duplicate][T N
T   S _Read_characters_as_STDIN][T  T   T   _Retrieve][S S S T  S T S S T   N
_Push_41_)][T   S S T   _Subtract][N
T   S S N
_If_0_Jump_to_Label_PRINT_TRAILING][S N
S _Duplicate][N
T   S T N
_If_0_Jump_to_Label_SKIP][S S S T   S T S S S N
_Push_40_(][T   N
S S _Print_as_character][N
S S T   N
_Create_Label_SKIP][S N
S _Duplicate][T T   T   _Retrieve][T    N
S S _Print_as_character][S S S T    N
_Push_1][T  S S S _Add][N
S N
N
_Jump_to_Label_LOOP][N
S S S N
_Create_Label_PRINT_TRAILING][S S S T   N
_Push_1][T  S S T   _Subtract][S N
S _Duplicate][N
T   S S S N
_If_0_Jump_to_Label_EXIT][S S S T   S T S S T   N
_Push_41_)][T   N
S S _Print_as_character][N
S N
S N
_Jump_to_Label_PRINT_TRAILING]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online (with raw spaces, tabs and new-lines only).

Since Whitespace's STDIN don't know when the input is done since it can only read a single character or number at a time, the input will need a trailing ) to indicate we're done inputting (which is possible since the challenge rules state: "For simplicity's sake, you may also assume the input will not contain parentheses").

Explanation in pseudo-code:

Integer n = 0
Start LOOP:
  Character c = read character from STDIN
  If(c == ')'):
    Jump to function PRINT_TRAILING
  If(n != 0)
    Print '(' to STDOUT
  Print c to STDOUT
  n = n + 1
  Jump to next iteration of LOOP

function PRINT_TRAILING:
  n = n - 1
  If(n == 0)
    Stop program
  Print ')' to STDOUT
  Jump to function PRINT_TRAILING
\$\endgroup\$
1
\$\begingroup\$

J, 29 bytes

f=:(')'#~<:@#),~}.@,@('(',.])

Try it online!

\$\endgroup\$
1
\$\begingroup\$

C++, 66 bytes

[&](string s){return s[1]?s.substr(0,1)+"("+f(s.substr(1))+")":s;}

Try it Online

Matlab, 45 bytes

@(s)fold(@(x,y)strcat(y,'(',x,')'),fliplr(s))

Try it Online!

The problem with this is that octave in try it online doesn't really support the fold function which of course exists in Matlab. Let me know if I should delete this solution since we can't test it on tio.

\$\endgroup\$
  • 1
    \$\begingroup\$ Being able to test a submission on TIO is not a requirement, so your MATLAB answer is just fine. It's preferable to post different solutions in different answers though, so they can be voted on independently. \$\endgroup\$ – Dennis Aug 28 '18 at 14:18
  • \$\begingroup\$ Octave implementation of fold() \$\endgroup\$ – ceilingcat Oct 16 '19 at 5:22
1
\$\begingroup\$

Racket 195 bytes

(let p((r "")(l(reverse(string->list s)))(i 0))(cond[(= i(length l))r][(= i 0)(set! r(string(list-ref l i)))
(p r l(+ 1 i))][(set! r(string-append(string(list-ref l i))"("r")" ))(p r l(+ 1 i))]))

Ungolfed:

(define (f s)
  (let loop ((r "")
             (l (reverse (string->list s)))
             (i 0))
    (cond
      [(>= i (length l)) r] 
      [(= i 0) (set! r (string (list-ref l i)))
               (loop r l (add1 i))]
      [else (set! r (string-append (string (list-ref l i)) "(" r ")" ))
            (loop r l (add1 i))]
      )))

Testing:

(f "Hello")

Output:

"H(e(l(l(o))))"

Edit: 2 bytes saved following suggestion by @JonathanFrech (add1 to + 1)

\$\endgroup\$
  • \$\begingroup\$ + 1 is shorter than add1. \$\endgroup\$ – Jonathan Frech Aug 28 '18 at 16:15
1
\$\begingroup\$

Gol><>, 21 bytes

TiE!tlF`(}}|~l2,R`)rH

Try it online!

24 bytes

TiE!trlF}8ss}|r~l2,R`)rH

It's hideous, I know. I am going to golf this profusely.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

C (gcc), 97 84 bytes

Thanks to ceilingcat for -13 bytes

f(char*b){printf(*++b?"%c(":"%c",*b);*b&&f(b)+printf(")");}a[99];main(){f(gets(a));}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Poetic, 244 bytes

life is a quest,i say
i choose a fun course to cross
i cant say quite if survival excites
i say i am laughing
i create a way i relive a feeling
exile is torture,i say
i am thankful,of course,to say i adore u
i desire a wedding where i said i do

Try it online!

Poetic is an esolang I made in 2018 for a class project. It's basically brainfuck with word-lengths instead of symbols.

The point of the language is to allow for programs to be written in free-verse poetry.

\$\endgroup\$
1
\$\begingroup\$

Keg, 22 16 15 12 bytes

?^⑷`(`⑸÷_(\)

Explained

?^⑷`(`⑸÷_(\)
#?^         Takes input and reverse it
#⑷`(`⑸÷    Maps an additional "(" to each letter
#÷_         Takes the last item and removes the extra bracket
#(\)        Appends a ")" for each item on the stack

Answer History

15 bytes

?!&("\()_^`)`&*

Try it online!

-1 byte due to some sort of stack-mechanic magic. I don't really know what I did, but it's shorter! Also, it's still ASCII only!

Explained:

?!&("\()_^`)`&*
?!&             #Take input and store the length in the register
   ("\()        #For each item on the stack, right shift and push a "("
        _^      #Pop the top and reverse
          `)`&* #Push ")" multiplied by the register (python-like string multiplication)

Answer History

16 bytes

?!&("\()'^_`)`&*

Try it online!

-6 bytes due to usage of the register rather than a custom variable. Also, that's 16 UTF-8/ASCII bytes for once.

Explained:

?!&("\()'^_`)`&*
?!&                 #Take input and store the length in the register
   ("\()            #For each item on the stack, right shift and push a "("
        '^_         #Left shift the stack, reverse and pop the top of stack
           `)`&*    #Push ")" multiplied by the register (python-like string multiplication)

22 bytes (SBCS)

?!®c("\()'^_(©c|\))^(,

Try it online!

Note that due to a newly discovered bug, TIO won't work properly, but the github interpreter will work correctly.

Explanation

#?!®c("\()'^_(©c|\))^(,
?!®c    #Get user input, and store the length in variable c
("\()   #For each item in the stack, right shift and push a "("
'^_ #Reverse the stack and pop the last most "("
(©c|\)) #For _ in range(0, var_c): append a ")"
^(, #Reverse and print the stack as characters
\$\endgroup\$
1
\$\begingroup\$

Wren, 54 bytes

Fn.new{|x|x.map{|i|i+"("}.join()[0..-2]+")"*~-x.count}

Try it online!

Wren, 36 bytes

I didn't write this myself. Therefore it is boring.

Fn.new{|x|x.join("(")+")"*~-x.count}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Befunge-93, 33 bytes

~# <\,"()"_v#+1:~,
         :,_@#

Try it online!

Due to the < character, the first line should be read backwards.

, : Output the most recently read character.

~ : Read a new character.

1+#v_: If there is no new character, go to the second line.

")(": Push parentheses characters onto the stack.

, : Output left parenthesis.

\ : Bury the right parenthesis deeper in the stack so it won't output until the end.

The rest of the first line is code to special-case the first character so that it gets output without creating parentheses.

The second line then simply outputs the stack until it's empty.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.