• Python Help Needed
    5 replies, posted
A problem from a book; [quote]You have to find the most frequently recurring quotient that results when a five-digit number divides exactly by the sum of its digits. For example, 82030 divides exactly by 13, which is the sum of its digits (8+2+0+3+0=13) to give a quotient of 6310. 63100 also divides exactly by 10 to give the same quotient, 6310. What is the quotient that most frequently occurs?[/quote] I figured this would be about building a list of applicable quotients, and then finding the statistical mode of the list. It's the second part I'm having trouble with now. I have: [PHP]q,n,li=0,0,[] for a in range(0,9): for b in range(0,9): for c in range(0,9): for d in range(0,9): for e in range(0,9): n=int(str(a)+str(b)+str(c)+str(d)+str(e)) dsum=(a+b+c+d+e) if dsum != 0: q=n/dsum if q==int(q): li.append(q)[/PHP] The starting (and rather cluttered, I'll tidy it later) series of loops create all the 5-digit numbers from 00000 to 99999. [I]n[/I] is used to contain this number (made from the concatenation of the loop indexes). The digit sum is stored in [I]dsum[/I] and (if it's not 0, to avoid /0 errors) is divided by the number [I]n[/I] to get the quotient [I]q[/I]. This quotient is (finally) appended to a list li[]. Now, I need to find the statistical mode for the list li[]. Halp, please?
[QUOTE=Pome;22726566][PHP]for a in range(0,9): for b in range(0,9): for c in range(0,9): for d in range(0,9): for e in range(0,9): n=int(str(a)+str(b)+str(c)+str(d)+str(e))[/PHP] The starting (and rather cluttered, I'll tidy it later) series of loops create all the 5-digit numbers from 00000 to 99999.[/QUOTE] oh my god are you actually serious?
[QUOTE=turb_;22726639]oh my god are you actually serious?[/QUOTE] Again, I'll tidy it later, I just built it quickly to give me the basis to continue.
You could just begin with for a in range(1,9) to avoid division by 0. 0xxxx would be a 4 digit number, so this would still keep all 5 digit numbers. Or do a for n in range(10000, 99999). To your actual problem: Use li.count(x).
[QUOTE=ZeekyHBomb;22726732]You could just begin with for a in range(1,9) to avoid division by 0. 0xxxx would be a 4 digit number, so this would still keep all 5 digit numbers. Or do a for n in range(10000, 99999). To your actual problem: Use li.count(x).[/QUOTE] Ah of course, 0xxxx would be four digits, I forgot about that. And now, I can't see why I didn't just do a simple for in range() and then seperately find the digit sum. To my actual problem: I'm not all too clear on how to go about using .count(), but I'll have a look, thanks.
Oh, and for iteration you can remove duplications via for n in sorted(list(li)). Python actually seems like a fun language. If our days were just 64h long. Or 128 while we're at it. Though that'd decrease our lifespan in years. Ah crap, you know what I mean :3
Sorry, you need to Log In to post a reply to this thread.