<div><div><div>"""</div><div>Mosaics using Python generators...</div><div>by K. Urner</div><div><a href="http://4dsolutions.net">4dsolutions.net</a></div><div>based on a thread on mathfuture:</div><div><a href="http://groups.google.com/group/mathfuture/msg/9d098696c2ea7426?hl=en">http://groups.google.com/group/mathfuture/msg/9d098696c2ea7426?hl=en</a></div>
<div><br></div><div>Just as the Fibonacci sequence generator might be "seeded" by any</div><div>two integers to start off, so might Pascal's Triangle be seeded with</div><div>any beginning row.</div><div><br>
</div><div>When these sequences are formed into tabular or other areal displays,</div><div>such that information may be gleaned from rows and columns, these</div><div>formats may be called "mosaics" in some of the literature.</div>
<div>"""</div><div><br></div><div>def fibonacci( a=0, b=1):</div><div> """</div><div> Generator for a Fibonacci sequence starting from any two integers</div><div><br></div><div> See: Online Encyclopedia of Integer Sequences</div>
<div> <a href="http://oeis.org/A000045">http://oeis.org/A000045</a></div><div> </div><div> >>> gen = fibonacci()</div><div> >>> [next(gen) for i in range(11)]</div><div> [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]</div>
<div> >>> gen = fibonacci(-1, 2)</div><div> >>> [next(gen) for i in range(11)]</div><div> [-1, 2, 1, 3, 4, 7, 11, 18, 29, 47, 76]</div><div> """</div><div><br></div><div> while True:</div>
<div> yield a</div><div> b, a = a + b, b</div><div><br></div><div><br></div><div>def pascal( therow = [1]):</div><div> """</div><div> Generator for Pascal's Triangle starting from any top row</div>
<div><br></div><div> Enter list for top row</div><div><br></div><div> See: Number Mosaics by <span class="Apple-style-span" style="font-family: Arial, sans-serif; font-size: 13px; white-space: nowrap; ">Adi R. Kanga</span> (pg. 29, Fig 20)</div>
<div> <a href="http://bit.ly/gDmmAo">http://bit.ly/gDmmAo</a></div><div> </div><div> >>> gen = pascal([15, 20, 6])</div><div> >>> next(gen)</div><div> [15, 20, 6]</div><div> >>> next(gen)</div>
<div> [15, 35, 26, 6]</div><div> >>> next(gen)</div><div> [15, 50, 61, 32, 6]</div><div> >>> next(gen)</div><div> [15, 65, 111, 93, 38, 6]</div><div> >>> next(gen)</div><div> [15, 80, 176, 204, 131, 44, 6]</div>
<div> >>> next(gen)</div><div> [15, 95, 256, 380, 335, 175, 50, 6]</div><div> """</div><div> while True:</div><div> yield therow</div><div> therow = [ i + j for i, j in zip(therow + [0], [0] + therow) ]</div>
<div> </div><div>def _test():</div><div> import doctest</div><div> doctest.testmod()</div><div><br></div><div>if __name__ == "__main__":</div><div> _test()</div><div> </div></div><div><br>
</div><div> </div></div>