Topographic Strings



Here is some example input, so I can explain what the problem is:

((1 2)(3 (4 5) moo)) (i (lik(cherries)e (woohoo)))

Think of this line of text as a topographic map of some mountains. Each set of parentheses illustrates one unit of altitude.

If we "view" this from the side, so that we see the mountains vertically, we will see:

          4 5                cherries    woohoo  
  1 2  3       moo       lik          e

Given one of these topographic maps, output the map, but on a vertical scale, like the output above. Seperate the different items in the map with the number of characters to the next item. For example, there are 4 spaces in the output between moo and i. Likewise, there are 4 characters in the input between moo and i.

The code which does this in the least amount of characters wins.


Posted 2012-07-12T01:37:32.730

Reputation: 3 904

Is it safe to assume that heights will always be positive? For example, the input ((1 2))))))))))3 should be invalid if negative heights are forbidden. – Cristian Lupascu – 2012-07-12T13:53:30.273

@w0lf: yep, the parentheses will always match up. – beary605 – 2012-07-12T13:56:14.613



J, 87 79 72 70 67 57 56 characters

'( ) 'charsub|.|:(+/\@('('&=-')'&=)(],~' '$~[)"0])1!:1[1

Takes input from the keyboard. Example:

   '( ) 'charsub|.|:(+/\@('('&=-')'&=)(],~' '$~[)"0])1!:1[1
((1 2)(3 (4 5) moo)) (i (lik(cherries)e (woohoo)))
          4 5                cherries    woohoo
  1 2  3       moo       lik          e


This explanation is based on the first version of my program:

|.|:('( ) 'charsub x)((' '$~{.@]),[{~{:@])"1(('('&([:+/=)-')'&([:+/=))\,.i.@#)x=.1!:1[1

x=.1!:1[1 take input from keyboard and put it into x for later

(('('&([:+/=)-')'&([:+/=))\,.i.@#) creates a list of all indeces into the string (i.@#) and stitches (,.) it together with the result of the (('('&([:+/=)-')'&([:+/=))\ verb.

(('('&([:+/=)-')'&([:+/=))\ this verb is applied to all the prefixes of the string (so on input hello it would apply to h,he,hel,hell, and hello. It is a fork, which counts the number of open brackets ('('&([:+/=) and then subtracts the number of close brackets ')'&([:+/=). This gives me list of indeces into the string and the level the character at that index should be at in the output. On simple input this gives me the following:

1  0
1  1
1  2
1  3
2  4
2  5
2  6
2  7
3  8
3  9
3 10
3 11
3 12
3 13
2 14
1 15
0 16

((' '$~{.@]),[{~{:@])"1 this is a verb which takes the list I just generated and also the output of ('( ) 'charsub x) (which just does a string replacement to replace all brackets with spaces in x). It takes the tail of each item of the list {:@] and uses it as an index into the string to get the character [{~{:@]. Then it prefixes it , with the number of spaces as indicated by the head of each item in the list(' '$~{.@]). On the previous example this gives me:

   ('( ) 'charsub x)((' '$~{.@]),[{~{:@])"1(('('&([:+/=)-')'&([:+/=))\,.i.@#)x=.1!:1[1




I then transpose the array |: and reverse it |. to get the desired output.


Posted 2012-07-12T01:37:32.730

Reputation: 11 678


GolfScript 69

0:§;{.'()'?))3%(.§+:§' ':s*\@s\if\n}%n/.{,}%$)\;:μ;{.,μ\-s*\+}%zip n*

Online demo here.


0:§;                # declare the variable §, representing the 
                    # current vertical level and initialize it at 0

{                   # iterate for each char in the string:

    .'()'?))3% (    # add on the stack the amount by which
                    # the current vertical level should be 
                    # adjusted:
                    #   * +1 if the character is '('
                    #   * -1 if the character is ')'
                    #   * 0 otherwise

    .§+:§           # adjust the value of §

    ' ':s*          # add as many spaces as § tells us
                    # and save the space in variable s

    \@s\if\         # return current char, if it's printable,
                    # or a space if it's '(' or ')'

    n               # add a newline char


n/                  # split by newline char; now we have 
                    # an array of strings on the stack.
                    # Each string is a vertical line of the
                    # final output.

.{,}%$)\;:μ;        # Iterate through the strings and find the
                    # maximum length

    .,μ\-s*\+       # Add spaces at the end to make all the strings 
                    # the same length

zip                 # Transpose the strings

n*                  # Join the transposed strings by newline characters

Cristian Lupascu

Posted 2012-07-12T01:37:32.730

Reputation: 8 369

@Gareth Yes, we both do :) – Cristian Lupascu – 2012-07-13T06:52:19.087

Care to add an explanation how it works? – Timwi – 2014-02-09T21:58:41.047

@Timwi I've edited my answer to include an explanation – Cristian Lupascu – 2014-02-10T07:59:13.887


Tcl, 50

puts \33\[9A[string map {( \33\[A ) \33\[B} $argv]

Kind of cheating, but well..

I use ascii escape sequences to get the line difference, ^[[A means move the curser 1 line up, ^[[B is move curser 1 line down.

Johannes Kuhn

Posted 2012-07-12T01:37:32.730

Reputation: 7 122


APL, 41 chars/bytes*


Tested on Dyalog, with a ⎕IO←1 and ⎕ML←3 environment. It's a function that takes the required input and returns the output. Given the question's wording, I believe it's acceptable. In case it's not, here is a version that reads from stdin and writes to stdout, for 4 chars more:



{                                 p←'()'}  p is the string made of two parentheses
                                ⍵∊ ______  check which characters from ⍵ are parens
                            1-2× ________  -1 for every par., 1 for every other char
                         ⍵/⍨ ____________  replace () with spaces in the orig. string
    (                 ),¨ _______________  append every char to the following items
                   ,\⍵ _____________________  for every prefix of the original string
               p∘.= ________________________  check which chars are '(' and which ')'
            +/¨ ____________________________  sum: compute the number of '(' and ')'
          -⌿ _______________________________  subtract the no. of ')' from that of '('
     ↑∘''¨ _________________________________  generate as many spaces as that number
 ⊖⍉⊃ ____________________________________  make it into a table, transpose and flip


topo '((1 2)(3 (4 5) moo)) (i (lik(cherries)e (woohoo)))'
          4 5                cherries    woohoo   
  1 2  3       moo       lik          e           


topo 'a  (  b ( c(d)e ) f  )  g'
          c   e          
      b           f      
a                       g

*: APL can be saved in a variety of legacy single-byte charsets that map APL symbols to the upper 128 bytes. Therefore, for the purpose of golfing, a program that only uses ASCII characters and APL symbols can be scored as chars = bytes.


Posted 2012-07-12T01:37:32.730

Reputation: 5 455

I'm searching the APL character set here and I can't find the symbol. It looks like a combination of the ¨ and ~ characters?

– Gareth – 2014-02-09T22:22:58.227

Hi @Gareth No, it was not in IBM APL2. You can find it in Dyalog (commercial, but there's a nagware version buried in their website, and that's good enough for golfing; IMHO the best APL available today), Nars2000 (best open source APL), GNU APL, and ngn's APL, among others.

– Tobia – 2014-02-09T22:46:14.227

@Gareth Graphically it's the combination of ~ and ¨, although it's a different character from both. It's an operator called Commute. In its dyadic form it flips the arguments of the dyadic function it's applied to: (5-2)=(2-⍨5). As a monadic operator it turns a dyadic function into monadic, duplicating the right argument: (2*2)=(*⍨2). It's mostly used to write an uninterrupted stream of functions from right to left, without having to put parentheses around large expressions and jump your eyes around them. In golfing it's useful because 3*⍨1-2 is one char less than (1-2)*3 :-) – Tobia – 2014-02-09T22:46:35.417

2So it's the equivalent of ~ in J then. – Gareth – 2014-02-09T23:37:35.877


APL (59)

⊖↑{⊃,/T\¨⍨⍵×P=0}¨R∘=¨(⍴T)∘⍴¨⍳⌈/R←1++\P←+/¨1 ¯1∘ר'()'∘=¨T←⍞

I have assumed that the 'base' needs to be usable as well. (i.e. (a(b))c(d) is valid). If this is not necessary two characters can be saved.


  • T←⍞: store a line of input in T
  • '()'∘=¨T: for each character in T, see if it is an opening or closing parenthesis. This gives a list of lists of booleans.
  • 1 ¯1∘ר: multiply the second element in each of these lists by -1 (so an opening parenthesis is 1, a closing one is -1 and any other character is 0).
  • +/¨: take the sum of each inner list. We now have the ∆y value for each character.
  • P←: store in P.
  • R←1++\P: take a running total of P, giving the height for each character. Add one to each character so that characters outside of the parentheses are on the first line.
  • (⍴T)∘⍴¨⍳⌈/R: for each possible y-value, make a list as long as T, consisting of only that value. (i.e. 1111..., 2222...., etc.)
  • R∘=¨: for each element in this list, see if it is equal to R. (For each level, we now have a list of zeroes and ones corresponding to whether or not a character should appear on that level).
  • ⍵×P=0: for each of these lists, set it to zero if P is not zero at that spot. This gets rid of the characters with a non-zero delta-y, so that gets rid of the parentheses.
  • ⊃,/T\¨⍨: for each depth, select from T the characters that should appear.
  • ⊖↑: create a matrix and put it right-side-up.


Posted 2012-07-12T01:37:32.730

Reputation: 30 224

Which APL implementation are you using? Is it free? – FUZxxl – 2012-07-23T08:35:41.977

@FUZxxl I've been using Dyalog APL, the Windows version can be downloaded for free. – marinus – 2012-07-23T14:23:28.513


J, 56 chars

'( ) 'charsub|.|:((,~#&' ')"0[:+/\1 _1 0{~'()'&i.)1!:1]1

Another 56-character J solution... I count depth by translating ( into ⁻1, ) into 1 and all other characters into 0, and then taking the running sum of this: [: +/\ 1 _1 0 {~ '()'&i.. The rest is largely similar to @Gareth's solution.


Posted 2012-07-12T01:37:32.730

Reputation: 7 107


C, 132 characters


The description failed to specify how much input the submission had to accept in order to be accepted, so I settled on limits that were most consistent with my golfing needs (while still working with the one given example input). Allow me to take this opportunity to remind folks that it's often a good idea to specify minimum maximums in your challenge descriptions.

There are two main loops in the code. The first loop farms out all the non-parenthesis characters to the appropriate line of output, and the second loop prints each line.


Posted 2012-07-12T01:37:32.730

Reputation: 6 893


Python, 161 chars

H=[S[:i].count('(')-S[:i].count(')')for i in R]+[0]
for h in range(max(H),0,-1):print''.join((' '+S[i])[H[i]==H[i+1]==h]for i in R)

Keith Randall

Posted 2012-07-12T01:37:32.730

Reputation: 19 865


Ruby 1.9 (129)

Reads from stdin.

$><<gets.split('').map{|c|x=[?(]*99;x[l+=c==?(?-1:c==?)?1:0]=c;x}*(?\n).tr('()',' ').gsub(/^\s+\n/,'')

Paul Prestidge

Posted 2012-07-12T01:37:32.730

Reputation: 2 390

3Nice! you discovered a bug in the Ruby highlighter :) – Cristian Lupascu – 2012-07-12T07:11:50.850

I've tested and SQL highlighting works better for your program. – Cristian Lupascu – 2012-07-12T07:12:12.940

@w0lf ha, you're right. I changed the // to '' which keeps the character count the same and avoids the bug in the highlighter. – Paul Prestidge – 2012-07-12T22:25:09.210


Python, 130

for c in raw_input():o=l;l+=c==')';l-=c=='(';a[l]=a[l].ljust(i)+c*(o==l);i+=1


Posted 2012-07-12T01:37:32.730

Reputation: 18 565


C, 149 chars

#define S for(i=0;c=v[1][i++];)h+=a=c-'('?c-')'?0:-1:1,
c,i,h=0,m=0;main(int a,char**v){S m=h>m?h:m;for(;m;m--){S putchar(a||h-m?32:c);putchar(10);}}

run with quoted arg, e.g. a.out "((1 2)(3 (4 5) moo)) (i (lik(cherries)e (woohoo)))"


Posted 2012-07-12T01:37:32.730

Reputation: 1 623


PowerShell, 120 119 bytes

(($h=($c=$args|% t*y)|%{($l+=(1,-1)[$_-40])})|sort)[-1]..0|%{$x=0;$y=$_
-join($c|%{"$_ "[$h[$x++]-ne$y-or$_-in40,41]})}

Try it online!

Side effects: Chars & and ' changes height as ( and ), but displays. Compare results for:

&$f "((1 2)(3 (4 5) moo)) (i (lik(cherries)e (woohoo)))"
&$f "&&1 2'&3 &4 5' moo'' &i &lik&cherries'e &woohoo'''"

Less golfed:

$chars=$args|% toCharArray

    $level+=(1,-1)[$_-40]       # 40 is ASCII for '(', 41 is ASCII for ')'


        "$_ "[$heights[$x++]-ne$y -or $_-in40,41]


Posted 2012-07-12T01:37:32.730

Reputation: 4 832


Octave, 128

Very similar to my last answer...

p=1;x=[0];y=input(0);for j=1:numel(y);p-=(y(j)==")");x(p,j)=y(j);p+=(y(j)=="(");end;x(x==40)=x(x==41)=x(x==0)=32;char(flipud(x))


Input: "((1 2)(3 (4 5) moo)) (i (lik(cherries)e (woohoo)))"


          4 5                cherries    woohoo   
  1 2  3       moo       lik          e           

sudo rm -rf slash

Posted 2012-07-12T01:37:32.730

Reputation: 1 076


C#, 229 bytes

If there's no restriction on leading vertical space, you can use this (indented for clarity.) It'll initialise the cursor down one line for every ( found before printing, then move the cursor up and down as brackets are read.

using C=System.Console;
class P{
    static void Main(string[]a){
        int l=a[0].Length,i=l;
            char c=a[0][i];
                c=' ';
                c=' ';


Posted 2012-07-12T01:37:32.730

Reputation: 7 912

-1 (For S&G)

Not the prettiest of code.

Module Q
 Sub Main(a As String())
  Dim t = a(0)
  Dim h = 0
  For Each m In (From G In (t.Select(Function(c)
                                     h += If(c = "(", 1, If(c = ")", -1, 0))
                                     Return h
                                   End Function).Select(Function(y, i) New With {.y = y, .i = i}))
             Group By G.y Into Group
             Order By   y Descending
            Select Group.ToDictionary(Function(x) x.i)
               ).Select(Function(d) New String(
                          t.Select(Function(c,i)If(d.ContainsKey(i),If(c="("c Or c=")"c," "c,c)," "c)).ToArray))
 End Sub
End Module

Adam Speight

Posted 2012-07-12T01:37:32.730

Reputation: 1 234