Python Forum
Split a number to list and list sum must be number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Split a number to list and list sum must be number
#1
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
Reply
#2
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.
sunny9495 likes this post
Reply
#3
(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
sunny9495 likes this post
Reply
#4
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.
Dexty and sunny9495 like this post
Reply
#5
(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")
Dexty likes this post
I speak Python but I don't speak English (I just read it a little). If I express myself badly, please blame the translator^^.
Reply
#6
(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
sunny9495 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Brick Number stored as text with openpyxl CAD79 1 218 Mar-19-2024, 08:31 PM
Last Post: deanhystad
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 288 Jan-27-2024, 05:23 PM
Last Post: Winfried
  Prime number detector Mark17 5 735 Nov-27-2023, 12:53 PM
Last Post: deanhystad
  Create X Number of Variables and Assign Data RockBlok 8 872 Nov-14-2023, 08:46 AM
Last Post: perfringo
  find the sum of a series of values that equal a number ancorte 1 461 Oct-30-2023, 05:41 AM
Last Post: Gribouillis
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  capturing multiline output for number of parameters jss 3 770 Sep-01-2023, 05:42 PM
Last Post: jss
  Sequential number for rows retrieved and storing the Primary UKey to the line number GYKR 2 555 Aug-22-2023, 10:14 AM
Last Post: GYKR
  doubling a number Skaperen 8 1,144 Jul-25-2023, 10:20 PM
Last Post: Skaperen
Question Extracting Version Number from a String britesc 2 1,030 May-31-2023, 10:20 AM
Last Post: britesc

Forum Jump:

User Panel Messages

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