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
#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


Messages In This Thread
RE: Split a number to list and list sum must be number - by Coricoco_fr - Apr-28-2022, 06:47 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing the code line number arbiel 2 160 Yesterday, 07:37 PM
Last Post: arbiel
  Finding the price based on industry and number of transactions chandramouliarun 1 1,072 Jun-04-2024, 06:57 PM
Last Post: marythodge4
Music Python Script Repeating Number When Saving Facebook Photos ThuanyPK 2 268 May-13-2024, 10:59 PM
Last Post: ebn852_pan
  Strange behavior list of list mmhmjanssen 3 506 May-09-2024, 11:32 AM
Last Post: mmhmjanssen
  intering int number akbarza 1 371 Apr-28-2024, 08:55 AM
Last Post: Gribouillis
Brick Number stored as text with openpyxl CAD79 2 744 Apr-17-2024, 10:17 AM
Last Post: CAD79
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 487 Jan-27-2024, 05:23 PM
Last Post: Winfried
  Prime number detector Mark17 5 969 Nov-27-2023, 12:53 PM
Last Post: deanhystad
  Create X Number of Variables and Assign Data RockBlok 8 1,186 Nov-14-2023, 08:46 AM
Last Post: perfringo
  find the sum of a series of values that equal a number ancorte 1 589 Oct-30-2023, 05:41 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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