Pyth is perhaps the most successful general-purpose golfing language. Though it is somewhat in decline as a result of newer languages, from 2014 to 2016 Pyth's concise syntax, constant updates, overloading, and (for its era) many builtins made it a favorite for the majority of questions.

Pyth code is often difficult to read. Even the output of the debug mode (transpiled Python) often consists of a long line, sometimes with parentheses nested ten deep. However, correctly formatted Pyth is very readable.

Here's a piece of Pyth code, written by @isaacg in Play the Word Chain.


It's much more readable like this.

.M                     Filter by gives-maximal-value of
   l Z                   lambda Z:length(Z) over
   f                     filter by (lambda T:
     q                     equal
       hM t T                head-map tail T
       eM P T                end-map Pop T)
     +L                    Append z to each element in
        s .pM y Q            flattened permutations of each subset of Q

For this challenge, we eliminate the aspect of categorizing Pyth characters and focus on formatting. Instead of being Pyth code, the input will consist of characters in 0123456789M. The digit n represents a function of arity n, and M represents an operator. For example, the above code is represented as 210221M101M102M011M10. Here are the steps to unminify:

Separate the string into tokens.

A token matches [0-9]M*. 0M will not occur in input.

Add trailing 0s.

When there are not enough arguments, Pyth appends as many implicit variables (lambda variables or Qs) to the code as are necessary to fill the arguments of the program; these should be represented by 0s.

Group tokens into lines.

The arity of a token is the value of its digit.

  • An arity-0 token (i.e. a 0) ends a line.

  • For an arity-1 token the next token should go on the same line, separated by a space.

  • For an arity >=2 token, its arguments go on separate lines, in the order they appear in the code, each followed by their own subarguments and so on. Arguments to a token are indented to the end of that token plus one space.


A nonempty string (or char array, array of length-1 strings, etc. as allowed by Standard I/O Methods) consisting of 0123456789M, which will not contain the substring 0M.


The string formatted according to the above rules.

Test cases


  1 0
      1M 1 0
      1M 1 0
       1 1M 1 0


1 2




1 1 0
1 1 1 0


  • \$\begingroup\$ Related question: codegolf.stackexchange.com/questions/47798/… \$\endgroup\$ – lirtosiast Nov 16 '18 at 17:54
  • \$\begingroup\$ Can I take the input as an array of digits/string? Example 210221M101M102M011M10 would be [2,1,0,2,2,1,'M',1,0,1,'M',1,0,2,'M',0,1,1,'M',1,0] \$\endgroup\$ – Luis felipe De jesus Munoz Nov 16 '18 at 18:29
  • \$\begingroup\$ @LuisfelipeDejesusMunoz No, unless standard I/O rules require that you be allowed to (which I don't think they do.) IMO it would slightly change the challenge if the Ms are allowed to be a different datatype from the integers. \$\endgroup\$ – lirtosiast Nov 16 '18 at 18:33
  • \$\begingroup\$ @lirtosiast So an array of characters/single-character strings is fine, just not using different datatypes between digits and M? \$\endgroup\$ – Kamil Drakari Nov 16 '18 at 19:54
  • 1
    \$\begingroup\$ @LeakyNun Empty string is now undefined behavior. \$\endgroup\$ – lirtosiast Nov 17 '18 at 19:29

JavaScript (ES8), 160 159 bytes

`.padEnd(P):' '):g(d--))(a[d]--,a[++d]=+c,p[d]=p[d-1]))+(d?f('0',a):'')

Try it online!


f = (                          // f = recursive function taking:
  s,                           //   s   = input string
  a = [                        //   a[] = array holding the number of expected arguments
    d = 0,                     //   d   = current depth, initialized to 0
    p = [1]                    //   p[] = array holding the padding values
  ]                            //
) =>                           //
  s.replace(                   // search in s all substrings
    RegExp('(.)M*', 'g'),      // consisting of a digit followed by 0 to N 'M' characters
    (s, c) =>                  // for each substring s beginning with the digit c:
      ( g = _ =>               //   g = recursive function
          a[d] ?               //     if we're still expecting at least one argument at
                               //     this depth:
            s + (              //       append s
              P = p[d] -=      //       update the padding value P = p[d] for this depth:
                c-- &&         //         decrement c; unless c was equal to 0,
                ~s.length,     //         add the length of s + 1 to p[d]
              c ?              //       if c is not equal to 0 (i.e. was not equal to 1):
                `\n`.padEnd(P) //         append a linefeed followed by P - 1 spaces
              :                //       else:
                ' '            //         append a single space
            )                  //
          :                    //     else (all arguments have been processed):
            g(d--)             //       decrement the depth and call g again
      )(                       //   before the initial call to g:
        a[d]--,                //     decrement the number of arguments at depth d
        a[++d] = +c,           //     set the number of arguments for the next depth
        p[d] = p[d - 1]        //     set the padding value for the next depth,
      )                        //     using a copy of the previous depth
  ) + (                        // end of replace()
    d ?                        // if we're not back at depth 0:
      f('0', a)                //   do a recursive call to f with an extra '0'
    :                          // else:
      ''                       //   stop recursion
  )                            //

Haskell, 192 190 187 bytes

f(n:r)|(m,t)<-span(>'9')r,(s,l)<-n#t=(s,n?((n:m):map((' '<$(n:n:m))++)l))
f e=(e,["0"])
'1'?(a:b:r)=(a++drop(length a)b):r
n#s|(r,l)<-f s=(l++)<$>pred n#r

Try it online!

There has to be a better way to handle the arity-1 case, it currently takes up 45 bytes.


  • -2 bytes by switching to a different method of handling 1, though the previous method has probably more optimization potential.
  • -3 bytes by not converting the character digits to numbers and using pred instead of n-1.
f(n:r)|(m,t)<-span(>'9')r,(s,l)<-read[n]#t,w<-map((' '<$(n:n:m))++)=(s,last$((n:m):w l):[(n:m++' ':h):w t|n<'2',h:t<-[l]])
f e=(e,["0"])
n#s|(r,l)<-f s=(l++)<$>(n-1)#r

Try it online!


Charcoal, 75 bytes


Try it online! Link is to verbose version of code. Explanation:


Loop over the input characters and turn them into a list of digits with optional M suffixes.


Reverse this list so that we can use Pop to consume it.


This variable is a stack of tokens whose arity has not yet been fulfilled.


This variable is a stack of the remaining arity of the unfulfilled tokens.


Repeat until we have consumed all of the tokens and emptied the stack.


Get the next token or 0 if none.


Print the token and then move the cursor horizontally if it begins with 1 otherwise diagonally.


Add the token and its arity to the appropriate variables.


Repeat while the arity stack is nonempty but the top arity is zero.


Discard the zero arity.


Remove its token and move that many characters left.


If there are any arities left then decrement the top arity.


Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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