Python Forum
HELP - Returning self numbers lower or equal to my argument - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: HELP - Returning self numbers lower or equal to my argument (/thread-13792.html)



HELP - Returning self numbers lower or equal to my argument - Kokuzuma - Nov-01-2018

Hello,

I am a student and a beginner in learning python coding, and I'm stuck on a self-numbers exercise, where I'm asked to return, from the ListeAuto(N) function, all self-numbers lower or equal to N with N belonging to N*.

But my function returns all the numbers between 1 and N, and I don't understand why :

def ListeAuto(N) :
    atnb=[]
    for k in range(1,N+1) :
        atnb.append(k)
        sommek=0
        for s in (list(int(c) for c in str(k))) :
            sommek+=k
        for n in atnb :
            if n==(sommek+k) :
                atnb.remove(n)
    return(atnb)
Can you help me finding the solution to my problem ? Thank you in advance.


RE: HELP - Returning self numbers lower or equal to my argument - ichabod801 - Nov-01-2018

Line 3 should be:

for k in range(1, N):
The range function returns an iterator through all the numbers starting at the first number and up to but not including the top number. So range(1, 5) goes through 1, 2, 3, and 4, but not 5. So by using N + 1, it was including N.


RE: HELP - Returning self numbers lower or equal to my argument - Kokuzuma - Nov-01-2018

But are you sure? Because I was asked to return slef numbers lower or "equal" to N. k therefore has to take N+1 as value in range function to ensure that the self numbers returned list will include N if it is one.
And the original problem remains ; the function, even replacing N+1 by N, will give me the list of all the numbers lower or equal to N-1, and not the list of self numbers specifically.


RE: HELP - Returning self numbers lower or equal to my argument - ichabod801 - Nov-01-2018

Sorry, I misunderstood the question. I think the problem is that you are not looking at the whole list. Take the first iteration of the loop. k = 1, atnb = []. You append 1 to atnb. You find that the sum of the digits in 1 is 1. Then you check all of the numbers in atnb to see if they equal 1 + 1. 1 is the only number in there, and it is not equal to 2. The next iteration k = 2 and atnb = [1]. You append 2 to atnb. But 2 should have been removed from atnb by 1.

The atnb list should start with all of the numbers from 1 to N. Then go through the numbers from 1 to N (independent of atnb), figure the sum of the number and all of it's digits, and remove that from atnb.

Have you worked with sets yet? They would make this much faster. Even without sets, the following would work better for removing numbers from atnb:

if n in atnb:
    atnb.remove(n)


Also, somnek can be written as somnek = sum(int(c) for c in str(k)). You might be able to avoid that calculation by noting that somnek(n+1) = somnek(n) + 2 - 9t, where t is the number of trailing zeroes in n+1.


RE: HELP - Returning self numbers lower or equal to my argument - Kokuzuma - Nov-01-2018

Yes, I followed your advice and I found the solution. Thank you.