Divide a number - Multiple levels - Sum of all numbers equal to input number - 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: Divide a number - Multiple levels - Sum of all numbers equal to input number (/thread-9001.html) Pages:
1
2
|
Divide a number - Multiple levels - Sum of all numbers equal to input number - pythoneer - Mar-16-2018 How can i divide a number multiple levels example: Quote:Number = 500 Quote:Step1: I am unable to write a nested for loop for this , i am able to get upto first level, from this number = 500 divide=[5,6,9] for i in divide: j=(number/sum(divide))*i print (number, j)Should i initiate a list for j and access each of them for next level? RE: Divide a number - Multiple levels - Sum of all numbers equal to input number - micseydel - Mar-16-2018 What have you tried? (Make sure to use code tags, and include any example input and output, including erroneous results / errors.) RE: Divide a number - Multiple levels - Sum of all numbers equal to input number - pythoneer - Mar-16-2018 (Mar-16-2018, 05:35 PM)micseydel Wrote: What have you tried? (Make sure to use code tags, and include any example input and output, including erroneous results / errors.) included the python code for first level RE: Divide a number - Multiple levels - Sum of all numbers equal to input number - nilamo - Mar-16-2018 So you've got j , which is (500/20) * each item of the list. Now that you have these three numbers, what should you do?
RE: Divide a number - Multiple levels - Sum of all numbers equal to input number - pythoneer - Mar-23-2018 i got this working with the following code Number = 500 Divide = [5,6,9] step1 = Number/sum(Divide) step2 = [step1 * i for i in Divide] for i in step2: # step3 print('{} {}'.format(Number, i)) step4 = [i / sum(Divide) for i in step2] step5 = [[i * j for i in step4] for j in Divide] k = 0 # step6 for i in step2: sum = 0 for j in step5[k]: sum = sum + j print('{} {} {}'.format(Number, i, sum)) k += 1[inline]Output: 500 125.0 31.25 500 125.0 68.75 500 125.0 125.0 500 150.0 37.5 500 150.0 82.5 500 150.0 150.0 500 225.0 56.25 500 225.0 123.75 500 225.0 225.0 [125.0, 150.0, 225.0] ##Step2 output [6.25, 7.5, 11.25] ##Step4 output [[31.25, 37.5, 56.25], [37.5, 45.0, 67.5], [56.25, 67.5, 101.25]] ##Step5 output[/inline] however i need to get the output slightly different now, i need to get the step5 output differently as follows [[31.25, 37.5, 56.25], [45.0, 67.5, 37.5], [101.25, 56.25, 67.5]] ##Step5 output the change here is the 2nd list in step5 list comprehension should start multiply from the 2nd number in divide list and go to 3rd number and then go to 1st number and 3rd list in step5 list comprehension should start multiply from the 3rd number and go to 1st number and 2nd number and so on RE: Divide a number - Multiple levels - Sum of all numbers equal to input number - pythoneer - Mar-24-2018 any help on this?? RE: Divide a number - Multiple levels - Sum of all numbers equal to input number - micseydel - Mar-24-2018 I'd appreciate it if you simplified your question. Exclude any and all code not related to the current issue you're trying to resolve. Include the actual vs desired output, as well as an explanation of how they differ. RE: Divide a number - Multiple levels - Sum of all numbers equal to input number - pythoneer - Mar-25-2018 Ok [inline]Expected output: 500 125.0 31.25 500 125.0 68.75 500 125.0 125.0 500 150.0 45 500 150.0 112.5 500 150.0 150.0 500 225.0 101.25 500 225.0 157.5 500 225.0 225.0[/inline] Python code tried for the above expected output : Number = 500 Divide = [5,6,9] step1 = Number/sum(Divide) step2 = [step1 * i for i in Divide] for i in step2: # step3 print('{} {}'.format(Number, i)) step4 = [i / sum(Divide) for i in step2] step5 = [[i * j for i in step4] for j in Divide] k = 0 # step6 for i in step2: sum = 0 for j in step5[k]: sum = sum + j print('{} {} {}'.format(Number, i, sum)) k += 1[inline]Output that is coming now 500 125.0 31.25 500 125.0 68.75 500 125.0 125.0 500 150.0 37.5 500 150.0 82.5 500 150.0 150.0 500 225.0 56.25 500 225.0 123.75 500 225.0 225.0[/inline] 1)We are dividing number 500 by sum of numbers in divide list i.e 20 2)Taking that value we are multiplying each number in divide list and getting answer as 125,150 and 225 3)Now we are dividing again these number by sum of numbers in divide list i.e 5+6+9 = 20 125/20 = 6.25 150/20 = 7.5 225/20 = 11.25 4)Now we have to multiply each of the above values in the order of numbers in Divide list 5*6.25 = 31.25 6*6.25 = 37.5 9*6.25 = 56.25 and display as 31.25,31.25+37.5,31.25+37.5+56.25 which is 31.25,68.75,125 5)For 2nd number in the answer we got in step 3 we need to start multiplying from 2nd number in divide list 6*7.5 = 45 9*7.5 = 67.5 5*7.5 = 37.5 and display as 45,45+67.5,45+67.5+37.5 which is 45,112.5,150 6)For 3rd number in the answer we got in step 3 we need to start multiplying from 3rd number in divide list 9*11.25 = 101.25 5*11.25 = 56.25 6*11.25 = 67.25 and display as 101.25,101.25+56.25,101.25+56.25+67.25 which is 101.25,157.5,225 RE: Divide a number - Multiple levels - Sum of all numbers equal to input number - Gribouillis - Mar-25-2018 Here is a way to produce the correct output. As always, it would be much better if you explained where the problem comes from and why you want to print the numbers in this order def gen_result(number, divisors): total = sum(divisors) for i, d in enumerate(divisors): acc = 0 num2 = number * d / total for d2 in divisors[i:] + divisors[:i]: acc += d2 yield [number, num2, num2 * acc / total] for item in gen_result(500, [5, 6, 9]): print('{} {} {}'.format(*item)) RE: Divide a number - Multiple levels - Sum of all numbers equal to input number - pythoneer - Mar-25-2018 could you please explain this a bit for my understanding and can i use this in a range of numbers and also what if i want to do the same calculation on numbers we got in step 4 to show 4 level of numbers |