Popular recipes tagged "builder"http://code.activestate.com/recipes/tags/builder/2012-03-03T17:50:03-08:00ActiveState Code RecipesSequence Builder (Python) 2012-03-03T17:50:03-08:00Thomas Lehmannhttp://code.activestate.com/recipes/users/4174477/http://code.activestate.com/recipes/578061-sequence-builder/ <p style="color: grey"> Python recipe 578061 by <a href="/recipes/users/4174477/">Thomas Lehmann</a> (<a href="/recipes/tags/builder/">builder</a>, <a href="/recipes/tags/sequence/">sequence</a>). Revision 2. </p> <p><strong>Why?</strong></p> <ul> <li>Thinking about a sequence of odd numbers where numbers divisible by 3 are not part of it I came to a sequence starting with 5, 7, 11, 13, 17, 19, ...</li> <li>The interesting property of this sequence is that the sequence of differences between the individual elements are 2,4,2,4,2,...</li> </ul> <p><strong>How?</strong></p> <ul> <li>I'm not a math expert (unfortunately) but I could imagine that there is a quite simple formula to get this.</li> <li>However I thought that using different combinations of defined functions a script could do the favour for me.</li> </ul> <p><strong>Result?</strong></p> <ul> <li>Formula: <code>(((-1)**(x-1))+1)+2</code> -> Simplified -> <code>-1**(x-1) + 3</code> (now clear to you?)</li> <li>You have to look for the sequence [4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2]</li> </ul> <p><strong>Out of scope:</strong></p> <ul> <li>Does not check for using lambda functions only and "x" only in the lambda functions.</li> <li>The Python script does not simplify functions like changing ((x+1)+1) to x+2 - would be an interesting recipe - by the way :)</li> </ul> <p><strong>Please note</strong></p> <ul> <li>You should not add other functions than lambda.</li> <li>You should always use "x" in your formular.</li> <li>Be aware that the actual setup runs a few minutes (about 3 minutes) and you can imagine - surely - that adding further functions will definitely increase the runtime.</li> </ul> <p><strong>Side effects?</strong></p> <ul> <li>Quite funny to produce a sequence of nines with <code>(((-1)**(2*x))+2)**2</code>.</li> <li>If you apply the first binomial theorem (a+b)^2 = a^2 + 2ab + b^2 then you see why!</li> </ul> <p><strong>What I have learned from this?</strong></p> <ul> <li>The biggest nightmare I did have with the Sequence class because of not knowing how to generate unique elements of this in a set (__eq__ and __hash__ are required); and I have to ensure that the hash code is calculated once only.</li> <li>The 'combineFunctions' was interesting to me. I have never used 'inspect.getsource' before.</li> <li>A few minutes does not sound much but for developing all times with more than 30 seconds are not comfortable. There are just 325 sequences and to investigate for certain sequences you probably have to have some more formula. Maybe I would have to take another approach for that.</li> </ul>