Posts: 54
Threads: 9
Joined: Mar 2018
Mar-16-2018, 05:33 PM
(This post was last modified: Mar-16-2018, 05:44 PM by pythoneer.)
How can i divide a number multiple levels
example:
Quote:Number = 500
Divide=[5,6,9]
Quote:Step1:
500/ (5+6+9) = 25
Step 2:
25 *5 = 125
25* 6 = 150
25* 9 = 225
Step 3
print
500 125
500 150
500 225
Step 4
Take 125 and divide that as follows
125 / (5+6+9) = 125/20 = 6.25
Step 5
6.25 * 5 = 31.25
6.25 * 6 = 37.50
6.25 * 9 = 56.25
Step 6
500 125 31.25
500 125 37.50
500 125 56.25
Step 7
Take 150 and 225 from step 3 and do the same thing from step 4 to step 6 and display
500 125 31.25
500 125 37.50
500 125 56.25
500 150 37.5
500 150 45
500 150 67.5
500 225 56.25
500 225 67.5
500 225 101.25
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?
Posts: 2,344
Threads: 62
Joined: Sep 2016
What have you tried? (Make sure to use code tags, and include any example input and output, including erroneous results / errors.)
Posts: 54
Threads: 9
Joined: Mar 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
Posts: 3,458
Threads: 101
Joined: Sep 2016
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?
Posts: 54
Threads: 9
Joined: Mar 2018
Mar-23-2018, 11:50 AM
(This post was last modified: Mar-23-2018, 11:51 AM by pythoneer.)
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
Posts: 54
Threads: 9
Joined: Mar 2018
Mar-24-2018, 01:38 AM
(This post was last modified: Mar-24-2018, 12:52 PM by pythoneer.)
Posts: 2,344
Threads: 62
Joined: Sep 2016
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.
Posts: 54
Threads: 9
Joined: Mar 2018
Mar-25-2018, 08:09 AM
(This post was last modified: Mar-25-2018, 08:09 AM by pythoneer.)
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
Posts: 4,498
Threads: 69
Joined: Jan 2018
Mar-25-2018, 08:30 AM
(This post was last modified: Mar-25-2018, 10:10 AM by Gribouillis.)
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))
Posts: 54
Threads: 9
Joined: Mar 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
|