<br><br><div class="gmail_quote">On Thu, Sep 30, 2010 at 3:20 AM, Rog <span dir="ltr"><<a href="mailto:rog@pynguins.com">rog@pynguins.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5">On Wed, 29 Sep 2010 05:52:32 -0700, <a href="mailto:bruno.desthuilliers@gmail.com">bruno.desthuilliers@gmail.com</a> wrote:<br>
<br>
> On 29 sep, 14:17, Steven D'Aprano <st...@REMOVE-THIS- <a href="http://cybersource.com.au" target="_blank">cybersource.com.au</a>><br>
> wrote:<br>
>> On Tue, 28 Sep 2010 20:11:51 +0100, Rog wrote:<br>
>> > On Tue, 28 Sep 2010 11:59:08 -0700, geremy condra wrote:<br>
>><br>
>> >> On Tue, Sep 28, 2010 at 11:44 AM, Rog <<a href="mailto:r...@pynguins.com">r...@pynguins.com</a>> wrote:<br>
>> >>> Hi all,<br>
>> >>> Have been grappling with a list problem for hours... a = [2, 3, 4,<br>
>> >>> 5,.....]<br>
>> >>> b = [4, 8, 2, 6,.....]<br>
>> >>> Basicly I am trying to place a[0], b[0] in a seperate list IF a[2]<br>
>> >>> and b[2] is present.<br>
>> >>> I have tried sets, zip etc with no success. I am tackling Euler<br>
>> >>> projects with Python 3.1, with minimal knowledge, and having to<br>
>> >>> tackle the language as I progress. Enjoyable frustration :)<br>
>><br>
>> >> I'm not clear on what your actual problem is, could you restate it?<br>
>><br>
>> >> It sounds like you want to copy the ith element out of a and b into<br>
>> >> some other list- call it c- when the (i+2)th element meets some<br>
>> >> condition. What's the condition?<br>
>><br>
>> >> Geremy Condra<br>
>><br>
>> > The condition is that the i-th element is inverted, but not equal. eg<br>
>> > 4,2 - 2,4 , 34,5 - 5,34 etc.<br>
>> > Hope that is clearer.<br>
>><br>
>> Clear as mud.<br>
>><br>
>> Perhaps you should given an example. Given input<br>
>><br>
>> a = [2, 3, 4, 5, 6, 7]<br>
>> b = [4, 8, 2, 6, 10, 42]<br>
>><br>
>> what output are you expecting,<br>
><br>
> AFAICT, the OP expects [2, 4] in this case, but it's not clear what he'd<br>
> expect for let's say:<br>
><br>
> a = [2, 3, 21, 4, 5, 6, 7]<br>
> b = [4, 8, 22, 2, 6, 10, 42]<br>
><br>
> (the 'reversed' pair is at i+3, not i+2)<br>
><br>
> or<br>
><br>
> a = [0, 2, 3, 4, 5, 6, 7]<br>
> b = [3, 4, 8, 2, 6, 10, 42]<br>
><br>
> (the first pair is at pos 1, not 0)<br>
><br>
> or<br>
><br>
> a = [2, 3, 4, 8, 6, 7]<br>
> b = [4, 8, 2, 3, 10, 42]<br>
><br>
> (there's a second 'non-reversed/reversed' match at positions resp. 1 & 3)<br>
<br>
<br>
</div></div>It is true that I would have needed any 'non-reversed/reversed' pairs,<br>
these would have been the amicable pairs I was looking for.<br>
The method to recognise them eluded me. Eyes are not good enough :)<br>
I have now joined Python-List and will follow the correct route.<br>
I have used a method suggested that has made the problem redundant,<br>
though it will bug me from now on.<br>
<br>
g = []<br>
<div class="im"><br>
def divsum(n):<br>
return sum(i for i in range(1, n) if not n % i)<br></div></blockquote><div><br></div><div>You can optimize divsum. Remember that factors exist in pairs except when numbers are squares.</div><div>Like say 12 have factors = 1, 12, 2, 6, 3, 4</div>
<div>so you can run the loop till sqrt(n) for n since floor(sqrt(n)) = 3 where n = 12</div><div>now factors are , (1, n/1), (2, n/2), (3, n/2).</div><div>for a square number say n = 9, the factors are (1, 3, 12)</div><div>
If you run the loop till sqrt(n) for n since floor(sqrt(n)) = 3 where n = 9 we get the factors as (1, 9), (3, 3)</div><div>One of the factor will be redundant, so you need to take care of that.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">
<br>
</div>for x in range(10000, 2, -1):<br>
c = divsum(x)<br>
v = divsum(c)<br></blockquote><div><br></div><div>Can be done as,</div><div>c, v = divsum(x), divsum(divsum(x))</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
if v == x and v != c:<br>
g.append(divsum(x))<br></blockquote><div><br></div><div>No need for else part, what is the use ?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
else:<br>
continue<br>
<br>
print(sum(g))<br></blockquote><div><br></div><div>You could pack this up as,</div><div><br></div><div>>>> sum(i for i in range(10000, 0, -2) if divsum(divsum(i)) == i and divsum(i) != i)</div><div>31626 </div><div>
<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im"><br>
--<br>
Rog<br>
<a href="http://www.rog.pynguins.com" target="_blank">http://www.rog.pynguins.com</a><br>
--<br>
</div><div><div></div><div class="h5"><a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>~l0nwlf<br>