<div dir="ltr">You need to put &quot;return&quot; in front of &quot;find_recurring_part(result[0], True)&quot;<br><br>Hope that helps,<br><br>&nbsp;- Jason<br><br><div class="gmail_quote">On Wed, Sep 3, 2008 at 11:57 AM, Benjamin Sergeant <span dir="ltr">&lt;<a href="mailto:bsergean@gmail.com">bsergean@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">== The background ==<br>
<br>
I&#39;m trying to solve problem 26 from project euler:<br>
<a href="http://projecteuler.net/index.php?section=problems&amp;id=26" target="_blank">http://projecteuler.net/index.php?section=problems&amp;id=26</a><br>
The goal is to find digits recurring cycle in rational number fractional parts.<br>
1 / 3 = 0.3333333 -&gt; 3 is a recuring integer<br>
1 / 7 = 0.142857<br>
<br>
== The python problem ==<br>
<br>
I have the following piece of code that try to solve that, probably<br>
not elegant but it kind of work. You can copy paste it and you should<br>
have the same output as I have, an assertion error: assert (<br>
recurring_cycle(3) == &#39;3&#39;). find_recurring_part() returns a, that is<br>
printed and equals 3, but when recurring_cycle get it, it has become<br>
None !!<br>
<br>
Does anyone know what&#39;s going on ?<br>
<br>
Thanks !<br>
- Benjamin.<br>
<br>
$ cat level26_strangeness.py<br>
#!/usr/bin/env python<br>
<br>
from decimal import Decimal, getcontext<br>
from re import findall<br>
<br>
def find_recurring_part(a, found):<br>
 &nbsp; &nbsp;&#39;&#39;&#39;<br>
 &nbsp; &nbsp;&gt;&gt;&gt; re.findall(r&#39;(\d+)\1&#39;, &#39;32323232&#39;)<br>
 &nbsp; &nbsp;[&#39;3232&#39;]<br>
 &nbsp; &nbsp;&#39;&#39;&#39;<br>
 &nbsp; &nbsp;result = findall(r&#39;(\d+)\1&#39;, a)<br>
 &nbsp; &nbsp;print a, result, found<br>
 &nbsp; &nbsp;if len(result) == 0:<br>
 &nbsp; &nbsp; &nbsp; &nbsp;return a if found else None # doing an old school if else does not help<br>
 &nbsp; &nbsp;else:<br>
 &nbsp; &nbsp; &nbsp; &nbsp;find_recurring_part(result[0], True)<br>
<br>
def recurring_cycle(i):<br>
 &nbsp; &nbsp;q = Decimal(1) / Decimal(i)<br>
 &nbsp; &nbsp;q = str(q)[2:]<br>
 &nbsp; &nbsp;return find_recurring_part(q, False)<br>
<br>
getcontext().prec = 10 # increase for bigger numbers<br>
assert ( recurring_cycle(2) == None)<br>
assert ( recurring_cycle(3) == &#39;3&#39;)<br>
<br>
$ ./level26_strangeness.py<br>
5 [] False<br>
3333333333 [&#39;33333&#39;] False<br>
33333 [&#39;33&#39;] True<br>
33 [&#39;3&#39;] True<br>
3 [] True<br>
Traceback (most recent call last):<br>
 &nbsp;File &quot;./level26_strangeness.py&quot;, line 25, in &lt;module&gt;<br>
 &nbsp; &nbsp;assert ( recurring_cycle(3) == &#39;3&#39;)<br>
AssertionError<br>
_______________________________________________<br>
Baypiggies mailing list<br>
<a href="mailto:Baypiggies@python.org">Baypiggies@python.org</a><br>
To change your subscription options or unsubscribe:<br>
<a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br>
</blockquote></div><br></div>