Python Forum
Split a number to list and list sum must be number - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Split a number to list and list sum must be number (/thread-37049.html)



Split a number to list and list sum must be number - sunny9495 - Apr-27-2022

How to split a number to list and list sum must be number

Example :

x = 8
i want to split and make a list with 3 items and the sum of list must be equal to x
y = [3, 3, 2]

Any help is appreciated


RE: Split a number to list and list sum must be number - ibreeden - Apr-27-2022

This smells like homework.
To start these kind of problems you must ask yourself: how should I do this by hand?
You would choose the first number. Then a second number and at last a third number and check if the sum equals "x". Then you would try all possibilities of the third number until you have a match. Or just calculate the third number.

You should also realize there must be limitations:
  • Is the number "0" allowed?
  • Are negative numbers allowed?
  • Do the numbers need to be integers?
  • Can a number be used more than once?
  • There may be more than one solution. How to handle that?

Please consider these thougts and try to make a script for it and let us know if you encounter problems.


RE: Split a number to list and list sum must be number - Dexty - Apr-27-2022

(Apr-27-2022, 05:10 AM)sunny9495 Wrote: How to split a number to list and list sum must be number

Example :

x = 8
i want to split and make a list with 3 items and the sum of list must be equal to x
y = [3, 3, 2]

Any help is appreciated
You should consider what @ibreeden has said. I tried something out though. You might want to check it out and how it works:

for i in range(x):
	for j in range(x):
		for k in range(x):
			if i + j + k == x:
				print([i, j, k])
...where x=8


RE: Split a number to list and list sum must be number - deanhystad - Apr-27-2022

The last loop is superfluous. The first loop should range from zero to x+1. The middle loop starts at i and ends at x-i+1. And there will be duplicates that must be culled. Theres got to be a better approach.


RE: Split a number to list and list sum must be number - Coricoco_fr - Apr-28-2022

(Apr-27-2022, 12:16 PM)Dexty Wrote:
(Apr-27-2022, 05:10 AM)sunny9495 Wrote: How to split a number to list and list sum must be number

Example :

x = 8
i want to split and make a list with 3 items and the sum of list must be equal to x
y = [3, 3, 2]

Any help is appreciated
You should consider what @ibreeden has said. I tried something out though. You might want to check it out and how it works:

for i in range(x):
	for j in range(x):
		for k in range(x):
			if i + j + k == x:
				print([i, j, k])
...where x=8

Hello,
for i in range(10):
	for j in range(10):
		for k in range(10):
			if i + j + k == 15:
				print(f"{i*100 + j*10 + k} ----> {i} + {j} + {k} =15")
import itertools
for i, j, k in itertools.product(range(10), range(10), range(10)):
        if i + j + k == 15:
                print(f"{i*100 + j*10 + k} ---->{i} + {j} + {k} =15")



RE: Split a number to list and list sum must be number - Dexty - Apr-28-2022

(Apr-28-2022, 06:47 AM)Coricoco_fr Wrote:
(Apr-27-2022, 12:16 PM)Dexty Wrote: You should consider what @ibreeden has said. I tried something out though. You might want to check it out and how it works:

for i in range(x):
	for j in range(x):
		for k in range(x):
			if i + j + k == x:
				print([i, j, k])
...where x=8

Hello,
for i in range(10):
	for j in range(10):
		for k in range(10):
			if i + j + k == 15:
				print(f"{i*100 + j*10 + k} ----> {i} + {j} + {k} =15")
import itertools
for i, j, k in itertools.product(range(10), range(10), range(10)):
        if i + j + k == 15:
                print(f"{i*100 + j*10 + k} ---->{i} + {j} + {k} =15")

Hmm. Can hardly go wrong with itertools