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.
sunny9495 and Dexty 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
  Control of the number of daemon error messages to the file MarPan 3 86 Mar-25-2026, 11:00 AM
Last Post: DeaD_EyE
  Exponential Number Conversion Tuurbo46 4 764 Jan-09-2026, 08:47 PM
Last Post: DeaD_EyE
  [split] print two different sequence number Reema 1 685 Nov-10-2025, 05:37 AM
Last Post: Gribouillis
  Send SMS from my phone number aster 4 4,788 May-11-2025, 05:48 PM
Last Post: TheTechNexus
Question [redistribution] Reduce number + size of dependencies? Winfried 2 1,343 Jan-31-2025, 10:17 PM
Last Post: snippsat
  Syntax for Doubling a number ksp_802 3 1,465 Jan-12-2025, 07:04 PM
Last Post: ksp_802
  Printing the code line number arbiel 6 2,548 Jun-30-2024, 08:01 AM
Last Post: arbiel
  Finding the price based on industry and number of transactions chandramouliarun 1 2,212 Jun-04-2024, 06:57 PM
Last Post: marythodge4
Music Python Script Repeating Number When Saving Facebook Photos ThuanyPK 2 1,635 May-13-2024, 10:59 PM
Last Post: ebn852_pan
  Strange behavior list of list mmhmjanssen 3 2,334 May-09-2024, 11:32 AM
Last Post: mmhmjanssen

Forum Jump:

User Panel Messages

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