julianaakkh - 1 year ago 69
Python Question

# Add up all posibilities in a list, with two variables

I am trying to make a program in python that will accept a user's input and check if it is a Kaprekar number.
I'm still a beginner, and have been having a lot of issues, but my main issue now that I can't seem to solve is how I would add up all posibilities in a list, with only two variables. I'm probably not explaning it very well so here is an example:

I have a list that contains the numbers

`['2', '0', '2', '5']`
.
How would I make python do
`2 + 025`
,
`20 + 25`
and
`202 + 5`
?

It would be inside an if else statement, and as soon as it would equal the user inputted number, it would stop.

Thanks

(Here is what the entire code looks like if it helps- where it currently says
`if 1 == 0:`
, it should be adding them up.)

``````a = ['2', '0', '2', '5']
``````

Then you can run

``````>>>  [(a[: i], a[i: ]) for i in range(1, len(a))]
[(['2'], ['0', '2', '5']), (['2', '0'], ['2', '5']), (['2', '0', '2'], ['5'])]
``````

to obtain all the possible contiguous splits.

If you want to process it further, you can change it to numbers via

``````>>> [(int(''.join(a[: i])), int(''.join(a[i: ]))) for i in range(1, len(a))]
[(2, 25), (20, 25), (202, 5)]
``````

``````>>> [int(''.join(a[: i])) + int(''.join(a[i: ])) for i in range(1, len(a))]