<div dir="ltr">You need to put "return" in front of "find_recurring_part(result[0], True)"<br><br>Hope that helps,<br><br> - Jason<br><br><div class="gmail_quote">On Wed, Sep 3, 2008 at 11:57 AM, Benjamin Sergeant <span dir="ltr"><<a href="mailto:bsergean@gmail.com">bsergean@gmail.com</a>></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'm trying to solve problem 26 from project euler:<br>
<a href="http://projecteuler.net/index.php?section=problems&id=26" target="_blank">http://projecteuler.net/index.php?section=problems&id=26</a><br>
The goal is to find digits recurring cycle in rational number fractional parts.<br>
1 / 3 = 0.3333333 -> 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) == '3'). 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'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>
'''<br>
>>> re.findall(r'(\d+)\1', '32323232')<br>
['3232']<br>
'''<br>
result = findall(r'(\d+)\1', a)<br>
print a, result, found<br>
if len(result) == 0:<br>
return a if found else None # doing an old school if else does not help<br>
else:<br>
find_recurring_part(result[0], True)<br>
<br>
def recurring_cycle(i):<br>
q = Decimal(1) / Decimal(i)<br>
q = str(q)[2:]<br>
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) == '3')<br>
<br>
$ ./level26_strangeness.py<br>
5 [] False<br>
3333333333 ['33333'] False<br>
33333 ['33'] True<br>
33 ['3'] True<br>
3 [] True<br>
Traceback (most recent call last):<br>
File "./level26_strangeness.py", line 25, in <module><br>
assert ( recurring_cycle(3) == '3')<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>