Best method to count the number of times the + operator is called during the evaluation of the function [on...












-3












$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.










share|improve this question







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.





















    -3












    $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.










    share|improve this question







    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.



















      -3












      -3








      -3





      $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.










      share|improve this question







      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






      share|improve this question







      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.











      share|improve this question







      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.









      share|improve this question




      share|improve this question






      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.






















          1 Answer
          1






          active

          oldest

          votes


















          0












          $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.






          share|improve this answer









          $endgroup$









          • 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




















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0












          $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.






          share|improve this answer









          $endgroup$









          • 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


















          0












          $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.






          share|improve this answer









          $endgroup$









          • 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
















          0












          0








          0





          $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.






          share|improve this answer









          $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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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 be n*i
            $endgroup$
            – Ludisposed
            11 hours ago
















          • 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










          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





          Popular posts from this blog

          How to make a Squid Proxy server?

          第一次世界大戦

          Touch on Surface Book