Python Forum
Divide a number - Multiple levels - Sum of all numbers equal to input number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Divide a number - Multiple levels - Sum of all numbers equal to input number
#1
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?
Reply
#2
What have you tried? (Make sure to use code tags, and include any example input and output, including erroneous results / errors.)
Reply
#3
(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
Reply
#4
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?
Reply
#5
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
Reply
#6
any help on this??
Reply
#7
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.
Reply
#8
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
Reply
#9
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))
Reply
#10
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create sum clusters of a number sequence BramQBIC 7 1,013 Nov-24-2023, 12:09 PM
Last Post: Pedroski55
Bug Program to check whether a number is palindrome or not PythonBoy 18 2,713 Sep-12-2023, 09:27 AM
Last Post: PythonBoy
  Trying to find the next prime number FirstBornAlbratross 8 4,338 Aug-14-2023, 01:16 PM
Last Post: deanhystad
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,419 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  Python Program to Find the Factorial of a Number elisahill 2 1,439 Nov-21-2022, 02:25 PM
Last Post: DeaD_EyE
  String to Number in Python Godjuned 3 1,358 Nov-17-2022, 07:22 AM
Last Post: Godjuned
  list digit into number Voldyy 2 1,539 Jul-10-2022, 06:13 PM
Last Post: deanhystad
  number to string Ali_ 1 1,281 Mar-31-2022, 11:22 AM
Last Post: ndc85430
  Checking the number of input Chrilo06 3 2,010 Mar-14-2022, 07:31 PM
Last Post: deanhystad
  Program that allows to accept only 10 integers but loops if an odd number was entered gachicardo 4 3,632 Feb-24-2022, 10:40 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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