#StackBounty: Generate Pyramid Scheme code

Bounty: 50

Pyramid Scheme is a language being developed by @ConorO’Brien. In Pyramid Scheme, the code that you write looks like this:

      ^         ^
     /        /3
    /         ---
   /  +  
  ^-------^
 /9     /3
/123    ---
-----

Now, that code has two obvious qualities: It’s difficult to parse, and it’s difficult to write. Conor has solved the first one, however it will be your job to solve that second issue.


The above code is processed by the PyramidScheme interpreter into a nested string array, like this:

[["+", ["9123", "3"]], "3"]

Your task is to write a program or function, which given a nested array of strings, outputs or returns the recreated PyramidScheme code. You may assume that the input array will always be valid.

A pyramid is an isosceles triangle. The top is ^, the sides slope diagonally away with / and , and the bottom is -. The two bottom corners are either empty or contain the start of other pyramids, which are arguments. The middle is filled with the pyramid’s name, ignoring line breaks.

Here’s how the parser converts the code into a useable format. First, it scans for a top-level pyramid. If it takes no arguments, it represents it with a single string and moves on. Otherwise, it represents is as an array ["name",[arg1,arg2]] or ["name",[arg1]]. The arguments are the pyramids at the bottom left and bottom right of the pyramid, which may be either string or more arrays described as above. You may notice that this somewhat resembles Lisp, in which case you may also have noticed the awful pun that is the language name. After the pyramid is fully represented, the parser moves on to the next one.

This is , shortest code wins!

Test Cases: These are not the only valid outputs, these are example of valid outputs.

[["+", ["9123", "3"]], "3"]

      ^         ^
     /        /3
    /         ---
   /  +  
  ^-------^
 /9     /3
/123    ---
-----

[["out", [["chr", ["72"]], ["chr", ["101"]]]], ["out", [["chr", ["108"]]]], ["out", [["chr", ["108"]]]], ["out", [["chr", ["111"]]]]]

        ^      ^     ^     ^
       /     /    /    / 
      /out  /out /out /out
     ^-----^ -----^----- -----^
    /    /     /          / 
   /chr /chr  /chr       /chr
  ^----- -----^ -----^     ^-----
 /          /     /    / 
/72        /101  /108 /111
-----       -----  ----- -----

[ ["+", [ ["asdfghjkl"], ["do", [ "1" ]] ]] ]

       ^
      / 
     / + 
    /     
   ^-------^
  /a     /d
 /sdf   /o  
/ghjkl ^-----
-------/1
       ---

Notice in the second test case, the second and third out pyramid both have a ["chr", ["108"]] as a parameter, which is collapsed into one pyramid stack shared by two top-level ones. This is a valid optimization your code may support, but it is completely optional; scoring is not based on the length of your output.

For the curious, the first case displays 9126 3 due to implicit printing of toplevel pyramids, the second one prints Hello, and the last one is a syntax error, included just because it has a neat structure.


You may assume that the input only contains printable ASCII, excluding spaces, ^, /, , and -. The input will always be valid, and contain at least one pyramid. There is no limit on the size of the array or the input strings, however you may write your code as if your language’s default integer type was infinite precision and that your computer has arbitrary memory. If taking input as a single string, you may use anything reasonable (comma, space, etc. as long as it’s in printable ascii and not " or []) to delimit arrays. You do not have to include brackets surrounding the entire thing, and instead take multiple arrays separated by your delimiter.

Your output does not have to be golfed, you may insert extra space or make your pyramids larger than necessary. Toplevel pyramids should be on the first line. Output should be a string with newlines or a list of strings.

Anyone who does include a version of their code which optimally golfs the pyramids may receive some rep in the form of upvotes/bounties (but probably just upvotes).


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.