Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not going to give you Objective-C code mainly because:</p> <ul> <li>I only know Objective-C on a very superficial level.</li> <li>I don't have the desire to write all the memory management code required to get this working in a language like C, and it would only detract from the readability anyway.</li> </ul> <p>Instead I will give you some ideas and some code showing how I would implement this in a higher language with generators and garbage collection (Python in this case) and a hint on how to do it without generators. Hopefully someone else may be able to port the code for you if you cannot do it yourself.</p> <p>I would think about your problem in a slightly different way:</p> <ul> <li>How many leading zeros are there in your initial "flushed-right" pattern.</li> <li>How many ways are there to partition that number of zeros into n partitions.</li> </ul> <p>In your last example you have two leading zeros and three partitions with separators '10' and '1':</p> <pre> 2 0 0: 00101 1 1 0: 01001 1 0 1: 01010 0 2 0: 10001 0 1 1: 10010 0 0 2: 10100 </pre> <p>The separators are always of the form <code>111..10</code> except the last which is just <code>111..1</code> without the trailing zero.</p> <p>To enumerate the above partitions use a function like the following in Python:</p> <pre><code>def partitions(n, x): if n == 1: yield [x] else: for i in range(x + 1): for p in partitions(n - 1, x - i): yield [i] + p for p in partitions(3, 2): print p </code></pre> <p>Result:</p> <pre><code>[0, 0, 2] [0, 1, 1] [0, 2, 0] [1, 0, 1] [1, 1, 0] [2, 0, 0] </code></pre> <p>Once you have these partitions it is simple to construct the patterns.</p> <p>One challenge is that Objective-C doesn't have built-in support for the yield construct. The following rewrite of the above function may be easier to convert to Objective-C:</p> <pre><code>def partitions(n, x): if n == 1: return [[x]] else: result = [] for i in range(x + 1): for p in partitions(n - 1, x - i): result.append([i] + p) return result </code></pre> <p>I hope that is of some use to you.</p>
    singulars
    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.
    2. 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