Best method to count the number of times the + operator is called during the evaluation of the function [on...
$begingroup$
def foo(n):
total = 0
for i in range(n):
for j in range(i):
total = total+i^2
return total
How best to modify this code to count the number of times the + operator is used during the evaluation of this function.
python-3.x
New contributor
Rian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
put on hold as off-topic by Heslacher, Ludisposed, Mast, Vogel612♦ 8 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Ludisposed, Mast, Vogel612
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
$begingroup$
def foo(n):
total = 0
for i in range(n):
for j in range(i):
total = total+i^2
return total
How best to modify this code to count the number of times the + operator is used during the evaluation of this function.
python-3.x
New contributor
Rian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
put on hold as off-topic by Heslacher, Ludisposed, Mast, Vogel612♦ 8 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Ludisposed, Mast, Vogel612
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
$begingroup$
def foo(n):
total = 0
for i in range(n):
for j in range(i):
total = total+i^2
return total
How best to modify this code to count the number of times the + operator is used during the evaluation of this function.
python-3.x
New contributor
Rian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
def foo(n):
total = 0
for i in range(n):
for j in range(i):
total = total+i^2
return total
How best to modify this code to count the number of times the + operator is used during the evaluation of this function.
python-3.x
python-3.x
New contributor
Rian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Rian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Rian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 12 hours ago
RianRian
11
11
New contributor
Rian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Rian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Rian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
put on hold as off-topic by Heslacher, Ludisposed, Mast, Vogel612♦ 8 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Ludisposed, Mast, Vogel612
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by Heslacher, Ludisposed, Mast, Vogel612♦ 8 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Ludisposed, Mast, Vogel612
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
You could just have a counter variable right after total = total + i ^ 2, and then display the counter however you want (printing, logging, etc):
def foo(n):
total = 0
counter = 0
for i in range(n):
for j in range(i):
total = total+i^2
counter = counter + 1
print("Times '+' evaluated: {}".format(counter))
return total
It may not be the most efficient way, but it gets the job done.
$endgroup$
4
$begingroup$
Please don't awnser off topic questions, and since the+operation is done for every iteration simply counts will ben*i
$endgroup$
– Ludisposed
11 hours ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
You could just have a counter variable right after total = total + i ^ 2, and then display the counter however you want (printing, logging, etc):
def foo(n):
total = 0
counter = 0
for i in range(n):
for j in range(i):
total = total+i^2
counter = counter + 1
print("Times '+' evaluated: {}".format(counter))
return total
It may not be the most efficient way, but it gets the job done.
$endgroup$
4
$begingroup$
Please don't awnser off topic questions, and since the+operation is done for every iteration simply counts will ben*i
$endgroup$
– Ludisposed
11 hours ago
add a comment |
$begingroup$
You could just have a counter variable right after total = total + i ^ 2, and then display the counter however you want (printing, logging, etc):
def foo(n):
total = 0
counter = 0
for i in range(n):
for j in range(i):
total = total+i^2
counter = counter + 1
print("Times '+' evaluated: {}".format(counter))
return total
It may not be the most efficient way, but it gets the job done.
$endgroup$
4
$begingroup$
Please don't awnser off topic questions, and since the+operation is done for every iteration simply counts will ben*i
$endgroup$
– Ludisposed
11 hours ago
add a comment |
$begingroup$
You could just have a counter variable right after total = total + i ^ 2, and then display the counter however you want (printing, logging, etc):
def foo(n):
total = 0
counter = 0
for i in range(n):
for j in range(i):
total = total+i^2
counter = counter + 1
print("Times '+' evaluated: {}".format(counter))
return total
It may not be the most efficient way, but it gets the job done.
$endgroup$
You could just have a counter variable right after total = total + i ^ 2, and then display the counter however you want (printing, logging, etc):
def foo(n):
total = 0
counter = 0
for i in range(n):
for j in range(i):
total = total+i^2
counter = counter + 1
print("Times '+' evaluated: {}".format(counter))
return total
It may not be the most efficient way, but it gets the job done.
answered 11 hours ago
David WhiteDavid White
336415
336415
4
$begingroup$
Please don't awnser off topic questions, and since the+operation is done for every iteration simply counts will ben*i
$endgroup$
– Ludisposed
11 hours ago
add a comment |
4
$begingroup$
Please don't awnser off topic questions, and since the+operation is done for every iteration simply counts will ben*i
$endgroup$
– Ludisposed
11 hours ago
4
4
$begingroup$
Please don't awnser off topic questions, and since the
+ operation is done for every iteration simply counts will be n*i$endgroup$
– Ludisposed
11 hours ago
$begingroup$
Please don't awnser off topic questions, and since the
+ operation is done for every iteration simply counts will be n*i$endgroup$
– Ludisposed
11 hours ago
add a comment |