Python Forum
HELP - Returning self numbers lower or equal to my argument
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HELP - Returning self numbers lower or equal to my argument
#1
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.
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Yes, I followed your advice and I found the solution. Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Higher or lower game SEWII 17 3,898 May-28-2023, 11:59 AM
Last Post: jefsummers
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,399 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  extract lower/upper antitriangular matrix schniefen 2 1,405 Dec-09-2022, 08:57 PM
Last Post: Gribouillis
  Convert list of numbers to string of numbers kam_uk 5 2,982 Nov-21-2020, 03:10 PM
Last Post: deanhystad
  Getting largest indices of array less than or equal to an array of numbers schniefen 5 2,614 Nov-02-2020, 08:14 PM
Last Post: schniefen
  HELP - Returning the first self number superior or equal to my argument Kokuzuma 1 2,125 Nov-01-2018, 08:22 PM
Last Post: ichabod801
  Divide a number - Multiple levels - Sum of all numbers equal to input number pythoneer 17 8,748 Apr-20-2018, 04:07 AM
Last Post: pythoneer
  Regular Expressions in Files (find all phone numbers and credit card numbers) Amirsalar 2 4,085 Dec-05-2017, 09:48 AM
Last Post: DeaD_EyE
  Function for returning absolute numbers Ernstblack 2 3,175 Oct-02-2017, 05:48 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020