Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are multiple ways to do this by structuring the index in a clever way involving a power of 2 somewhere as expected but as always python has one of the cleanest representation albeit somewhat backward</p> <pre><code>def sierpinski(n): d = ["*"] for i in xrange(n): sp = " " * (2 ** i) d = [sp+x+sp for x in d] + [x+" "+x for x in d] return d print "\n".join(sierpinski(4)) </code></pre> <p>The trick is to realize it's not printing the * that makes up the triangle but all the spaces as well, change " " in the code to "c" and you will see the following. Idea is it starts with a basic pattern and keeps appending to its head and tail so you start with 2 elements, then 4, then 8, finally 16. and since python prints list with newline delimiters you naturally get the pattern you are looking for. </p> <pre><code>sp is c ['c*c', '*c*'] sp is cc ['ccc*ccc', 'cc*c*cc', 'c*ccc*c', '*c*c*c*'] sp is cccc ['ccccccc*ccccccc', 'cccccc*c*cccccc', 'ccccc*ccc*ccccc', 'cccc*c*c*c*cccc', 'ccc*ccccccc*ccc', 'cc*c*ccccc*c*cc', 'c*ccc*ccc*ccc*c', '*c*c*c*c*c*c*c*'] sp is cccccccc ['ccccccccccccccc*ccccccccccccccc', 'cccccccccccccc*c*cccccccccccccc', 'ccccccccccccc*ccc*ccccccccccccc', 'cccccccccccc*c*c*c*cccccccccccc', 'ccccccccccc*ccccccc*ccccccccccc', 'cccccccccc*c*ccccc*c*cccccccccc', 'ccccccccc*ccc*ccc*ccc*ccccccccc', 'cccccccc*c*c*c*c*c*c*c*cccccccc', 'ccccccc*ccccccccccccccc*ccccccc', 'cccccc*c*ccccccccccccc*c*cccccc', 'ccccc*ccc*ccccccccccc*ccc*ccccc', 'cccc*c*c*c*ccccccccc*c*c*c*cccc', 'ccc*ccccccc*ccccccc*ccccccc*ccc', 'cc*c*ccccc*c*ccccc*c*ccccc*c*cc', 'c*ccc*ccc*ccc*ccc*ccc*ccc*ccc*c', '*c*c*c*c*c*c*c*c*c*c*c*c*c*c*c*'] ccccccccccccccc*ccccccccccccccc cccccccccccccc*c*cccccccccccccc ccccccccccccc*ccc*ccccccccccccc cccccccccccc*c*c*c*cccccccccccc ccccccccccc*ccccccc*ccccccccccc cccccccccc*c*ccccc*c*cccccccccc ccccccccc*ccc*ccc*ccc*ccccccccc cccccccc*c*c*c*c*c*c*c*cccccccc ccccccc*ccccccccccccccc*ccccccc cccccc*c*ccccccccccccc*c*cccccc ccccc*ccc*ccccccccccc*ccc*ccccc cccc*c*c*c*ccccccccc*c*c*c*cccc ccc*ccccccc*ccccccc*ccccccc*ccc cc*c*ccccc*c*ccccc*c*ccccc*c*cc c*ccc*ccc*ccc*ccc*ccc*ccc*ccc*c *c*c*c*c*c*c*c*c*c*c*c*c*c*c*c* </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload