Function that adds two items from a list in search of those that will match some given sum
up vote
0
down vote
favorite
I'm doing a Daily Challenges to get better with Python.
I completed the first challenges with a working code but I want to make sure if there's anyway to improve or optimize it.
I have a function that adds 2 items from a list in search of those that will match the already defined result.
def main():
listA = [10,11,3,7]
k = 21
for i in listA:
for e in listA:
result = i + e
if result == k:
print(i, '+', e, ' = ', k)
return
main()
I was thinking of reducing it to:
def main():
k = 21
listA=[10,11,3,7]
print (k,'=',[(i and e) for i in listA for e in listA if k==(i+e)])
main()
… but that would print 21 = [10,11] instead of 21 = 10 + 11.
python python-3.x programming-challenge
add a comment |
up vote
0
down vote
favorite
I'm doing a Daily Challenges to get better with Python.
I completed the first challenges with a working code but I want to make sure if there's anyway to improve or optimize it.
I have a function that adds 2 items from a list in search of those that will match the already defined result.
def main():
listA = [10,11,3,7]
k = 21
for i in listA:
for e in listA:
result = i + e
if result == k:
print(i, '+', e, ' = ', k)
return
main()
I was thinking of reducing it to:
def main():
k = 21
listA=[10,11,3,7]
print (k,'=',[(i and e) for i in listA for e in listA if k==(i+e)])
main()
… but that would print 21 = [10,11] instead of 21 = 10 + 11.
python python-3.x programming-challenge
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm doing a Daily Challenges to get better with Python.
I completed the first challenges with a working code but I want to make sure if there's anyway to improve or optimize it.
I have a function that adds 2 items from a list in search of those that will match the already defined result.
def main():
listA = [10,11,3,7]
k = 21
for i in listA:
for e in listA:
result = i + e
if result == k:
print(i, '+', e, ' = ', k)
return
main()
I was thinking of reducing it to:
def main():
k = 21
listA=[10,11,3,7]
print (k,'=',[(i and e) for i in listA for e in listA if k==(i+e)])
main()
… but that would print 21 = [10,11] instead of 21 = 10 + 11.
python python-3.x programming-challenge
I'm doing a Daily Challenges to get better with Python.
I completed the first challenges with a working code but I want to make sure if there's anyway to improve or optimize it.
I have a function that adds 2 items from a list in search of those that will match the already defined result.
def main():
listA = [10,11,3,7]
k = 21
for i in listA:
for e in listA:
result = i + e
if result == k:
print(i, '+', e, ' = ', k)
return
main()
I was thinking of reducing it to:
def main():
k = 21
listA=[10,11,3,7]
print (k,'=',[(i and e) for i in listA for e in listA if k==(i+e)])
main()
… but that would print 21 = [10,11] instead of 21 = 10 + 11.
python python-3.x programming-challenge
python python-3.x programming-challenge
edited Nov 27 at 0:04
Gerrit0
2,9191522
2,9191522
asked Nov 26 at 19:58
Mark-André Tremblay
32
32
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
def main():
k = 21
listA=[10,11,3,7]
print (k,' = '," + ".join([str(i and e) for i in listA for e in listA if k==(i+e)]))
main()
This minor change to your code will do what you're after. Put a " + ".join( ) around your list creator and cast the (i and e) items to str, that will then join all string elements in the list together into a string with + as the separator (there are spaces in there, yes, and this is necessary to get the output you're after).
Proof of Concept (via tio.run) that shows you how this will execute.
Note I also made a change to add spaces around the equals sign, indentation, and extra spacing for readability, as well as indentation standardization (4 spaces for a single indentation level).
Thanks, It worked perfectly. the .join() is that a built-in function in Python? Like append?
– Mark-André Tremblay
Nov 26 at 21:59
@Mark-AndréTremblay it's a string function, a builtin as part of thestrdatatype. (Python documentation for 3.7 on str.join is better probably for answering your question)
– Thomas Ward
Nov 27 at 1:35
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
def main():
k = 21
listA=[10,11,3,7]
print (k,' = '," + ".join([str(i and e) for i in listA for e in listA if k==(i+e)]))
main()
This minor change to your code will do what you're after. Put a " + ".join( ) around your list creator and cast the (i and e) items to str, that will then join all string elements in the list together into a string with + as the separator (there are spaces in there, yes, and this is necessary to get the output you're after).
Proof of Concept (via tio.run) that shows you how this will execute.
Note I also made a change to add spaces around the equals sign, indentation, and extra spacing for readability, as well as indentation standardization (4 spaces for a single indentation level).
Thanks, It worked perfectly. the .join() is that a built-in function in Python? Like append?
– Mark-André Tremblay
Nov 26 at 21:59
@Mark-AndréTremblay it's a string function, a builtin as part of thestrdatatype. (Python documentation for 3.7 on str.join is better probably for answering your question)
– Thomas Ward
Nov 27 at 1:35
add a comment |
up vote
1
down vote
accepted
def main():
k = 21
listA=[10,11,3,7]
print (k,' = '," + ".join([str(i and e) for i in listA for e in listA if k==(i+e)]))
main()
This minor change to your code will do what you're after. Put a " + ".join( ) around your list creator and cast the (i and e) items to str, that will then join all string elements in the list together into a string with + as the separator (there are spaces in there, yes, and this is necessary to get the output you're after).
Proof of Concept (via tio.run) that shows you how this will execute.
Note I also made a change to add spaces around the equals sign, indentation, and extra spacing for readability, as well as indentation standardization (4 spaces for a single indentation level).
Thanks, It worked perfectly. the .join() is that a built-in function in Python? Like append?
– Mark-André Tremblay
Nov 26 at 21:59
@Mark-AndréTremblay it's a string function, a builtin as part of thestrdatatype. (Python documentation for 3.7 on str.join is better probably for answering your question)
– Thomas Ward
Nov 27 at 1:35
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
def main():
k = 21
listA=[10,11,3,7]
print (k,' = '," + ".join([str(i and e) for i in listA for e in listA if k==(i+e)]))
main()
This minor change to your code will do what you're after. Put a " + ".join( ) around your list creator and cast the (i and e) items to str, that will then join all string elements in the list together into a string with + as the separator (there are spaces in there, yes, and this is necessary to get the output you're after).
Proof of Concept (via tio.run) that shows you how this will execute.
Note I also made a change to add spaces around the equals sign, indentation, and extra spacing for readability, as well as indentation standardization (4 spaces for a single indentation level).
def main():
k = 21
listA=[10,11,3,7]
print (k,' = '," + ".join([str(i and e) for i in listA for e in listA if k==(i+e)]))
main()
This minor change to your code will do what you're after. Put a " + ".join( ) around your list creator and cast the (i and e) items to str, that will then join all string elements in the list together into a string with + as the separator (there are spaces in there, yes, and this is necessary to get the output you're after).
Proof of Concept (via tio.run) that shows you how this will execute.
Note I also made a change to add spaces around the equals sign, indentation, and extra spacing for readability, as well as indentation standardization (4 spaces for a single indentation level).
edited Nov 27 at 1:37
answered Nov 26 at 20:55
Thomas Ward
1,773823
1,773823
Thanks, It worked perfectly. the .join() is that a built-in function in Python? Like append?
– Mark-André Tremblay
Nov 26 at 21:59
@Mark-AndréTremblay it's a string function, a builtin as part of thestrdatatype. (Python documentation for 3.7 on str.join is better probably for answering your question)
– Thomas Ward
Nov 27 at 1:35
add a comment |
Thanks, It worked perfectly. the .join() is that a built-in function in Python? Like append?
– Mark-André Tremblay
Nov 26 at 21:59
@Mark-AndréTremblay it's a string function, a builtin as part of thestrdatatype. (Python documentation for 3.7 on str.join is better probably for answering your question)
– Thomas Ward
Nov 27 at 1:35
Thanks, It worked perfectly. the .join() is that a built-in function in Python? Like append?
– Mark-André Tremblay
Nov 26 at 21:59
Thanks, It worked perfectly. the .join() is that a built-in function in Python? Like append?
– Mark-André Tremblay
Nov 26 at 21:59
@Mark-AndréTremblay it's a string function, a builtin as part of the
str datatype. (Python documentation for 3.7 on str.join is better probably for answering your question)– Thomas Ward
Nov 27 at 1:35
@Mark-AndréTremblay it's a string function, a builtin as part of the
str datatype. (Python documentation for 3.7 on str.join is better probably for answering your question)– Thomas Ward
Nov 27 at 1:35
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
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%2fcodereview.stackexchange.com%2fquestions%2f208471%2ffunction-that-adds-two-items-from-a-list-in-search-of-those-that-will-match-some%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