Python: combination calculator to targeted sum - validation step
up vote
0
down vote
favorite
I'm still struggling with this code. It's a number combination calculator to targeted sum. I need to add a validation step to see if the number already exists in my output list - if it exists then the code shouldn’t show this output. I try a lot of ideas, but I can’t solve it (for example if x in numbers etc.). Thank you so much for the advice.
def subset_sum(numbers, target, partial=):
s = sum([int(x) for x in partial])
numbers = [int(x.strip()) for x in combination.split(',')]
if s == int(target):
print("sum(%s)=%s" % (partial, int(target)))
if s >= int(target):
return
for i in range(len(numbers)):
n = numbers[i]
remaining = numbers[i + 1:]
subset_sum(remaining, int(target), partial + [n])
combination = input("combination: ")
target_sum = input("target: ")
if __name__ == "__main__":
subset_sum([combination], target_sum)
python python-3.x
add a comment |
up vote
0
down vote
favorite
I'm still struggling with this code. It's a number combination calculator to targeted sum. I need to add a validation step to see if the number already exists in my output list - if it exists then the code shouldn’t show this output. I try a lot of ideas, but I can’t solve it (for example if x in numbers etc.). Thank you so much for the advice.
def subset_sum(numbers, target, partial=):
s = sum([int(x) for x in partial])
numbers = [int(x.strip()) for x in combination.split(',')]
if s == int(target):
print("sum(%s)=%s" % (partial, int(target)))
if s >= int(target):
return
for i in range(len(numbers)):
n = numbers[i]
remaining = numbers[i + 1:]
subset_sum(remaining, int(target), partial + [n])
combination = input("combination: ")
target_sum = input("target: ")
if __name__ == "__main__":
subset_sum([combination], target_sum)
python python-3.x
to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. StackOverflow is not a coding, review, or tutorial resource.
– Prune
Nov 19 at 21:50
In what way didif x in numbers
not work? It sounds like a reasonable approach for checking if a number exists in a list.
– mkrieger1
Nov 19 at 22:23
@mkrieger1 Forif x in numbers
I need to definex
but I don't know how to do it. Ifx
is in this codenumbers = [int(x.strip()) for x in combination.split(',')]
It will be x = int(x)? Thank you
– Miroslav Navrátil
Nov 24 at 7:47
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm still struggling with this code. It's a number combination calculator to targeted sum. I need to add a validation step to see if the number already exists in my output list - if it exists then the code shouldn’t show this output. I try a lot of ideas, but I can’t solve it (for example if x in numbers etc.). Thank you so much for the advice.
def subset_sum(numbers, target, partial=):
s = sum([int(x) for x in partial])
numbers = [int(x.strip()) for x in combination.split(',')]
if s == int(target):
print("sum(%s)=%s" % (partial, int(target)))
if s >= int(target):
return
for i in range(len(numbers)):
n = numbers[i]
remaining = numbers[i + 1:]
subset_sum(remaining, int(target), partial + [n])
combination = input("combination: ")
target_sum = input("target: ")
if __name__ == "__main__":
subset_sum([combination], target_sum)
python python-3.x
I'm still struggling with this code. It's a number combination calculator to targeted sum. I need to add a validation step to see if the number already exists in my output list - if it exists then the code shouldn’t show this output. I try a lot of ideas, but I can’t solve it (for example if x in numbers etc.). Thank you so much for the advice.
def subset_sum(numbers, target, partial=):
s = sum([int(x) for x in partial])
numbers = [int(x.strip()) for x in combination.split(',')]
if s == int(target):
print("sum(%s)=%s" % (partial, int(target)))
if s >= int(target):
return
for i in range(len(numbers)):
n = numbers[i]
remaining = numbers[i + 1:]
subset_sum(remaining, int(target), partial + [n])
combination = input("combination: ")
target_sum = input("target: ")
if __name__ == "__main__":
subset_sum([combination], target_sum)
python python-3.x
python python-3.x
asked Nov 19 at 21:46
Miroslav Navrátil
12
12
to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. StackOverflow is not a coding, review, or tutorial resource.
– Prune
Nov 19 at 21:50
In what way didif x in numbers
not work? It sounds like a reasonable approach for checking if a number exists in a list.
– mkrieger1
Nov 19 at 22:23
@mkrieger1 Forif x in numbers
I need to definex
but I don't know how to do it. Ifx
is in this codenumbers = [int(x.strip()) for x in combination.split(',')]
It will be x = int(x)? Thank you
– Miroslav Navrátil
Nov 24 at 7:47
add a comment |
to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. StackOverflow is not a coding, review, or tutorial resource.
– Prune
Nov 19 at 21:50
In what way didif x in numbers
not work? It sounds like a reasonable approach for checking if a number exists in a list.
– mkrieger1
Nov 19 at 22:23
@mkrieger1 Forif x in numbers
I need to definex
but I don't know how to do it. Ifx
is in this codenumbers = [int(x.strip()) for x in combination.split(',')]
It will be x = int(x)? Thank you
– Miroslav Navrátil
Nov 24 at 7:47
to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. StackOverflow is not a coding, review, or tutorial resource.
– Prune
Nov 19 at 21:50
to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. StackOverflow is not a coding, review, or tutorial resource.
– Prune
Nov 19 at 21:50
In what way did
if x in numbers
not work? It sounds like a reasonable approach for checking if a number exists in a list.– mkrieger1
Nov 19 at 22:23
In what way did
if x in numbers
not work? It sounds like a reasonable approach for checking if a number exists in a list.– mkrieger1
Nov 19 at 22:23
@mkrieger1 For
if x in numbers
I need to define x
but I don't know how to do it. If x
is in this code numbers = [int(x.strip()) for x in combination.split(',')]
It will be x = int(x)? Thank you– Miroslav Navrátil
Nov 24 at 7:47
@mkrieger1 For
if x in numbers
I need to define x
but I don't know how to do it. If x
is in this code numbers = [int(x.strip()) for x in combination.split(',')]
It will be x = int(x)? Thank you– Miroslav Navrátil
Nov 24 at 7:47
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383094%2fpython-combination-calculator-to-targeted-sum-validation-step%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. StackOverflow is not a coding, review, or tutorial resource.
– Prune
Nov 19 at 21:50
In what way did
if x in numbers
not work? It sounds like a reasonable approach for checking if a number exists in a list.– mkrieger1
Nov 19 at 22:23
@mkrieger1 For
if x in numbers
I need to definex
but I don't know how to do it. Ifx
is in this codenumbers = [int(x.strip()) for x in combination.split(',')]
It will be x = int(x)? Thank you– Miroslav Navrátil
Nov 24 at 7:47