Counting output









up vote
1
down vote

favorite












def leap_years(start, end):
if start < 1500 or start > 2100:
return
if end < 1500 or end > 2100:
return
i = 0
for i in range(start, end + 1):
if i % 4 == 0 and (i % 100 != 0 or i % 400 == 0):
print(i)


print(leap_years(1998, 2008))


I want to output be 3 and not 2000,2004,2008










share|improve this question



























    up vote
    1
    down vote

    favorite












    def leap_years(start, end):
    if start < 1500 or start > 2100:
    return
    if end < 1500 or end > 2100:
    return
    i = 0
    for i in range(start, end + 1):
    if i % 4 == 0 and (i % 100 != 0 or i % 400 == 0):
    print(i)


    print(leap_years(1998, 2008))


    I want to output be 3 and not 2000,2004,2008










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      def leap_years(start, end):
      if start < 1500 or start > 2100:
      return
      if end < 1500 or end > 2100:
      return
      i = 0
      for i in range(start, end + 1):
      if i % 4 == 0 and (i % 100 != 0 or i % 400 == 0):
      print(i)


      print(leap_years(1998, 2008))


      I want to output be 3 and not 2000,2004,2008










      share|improve this question















      def leap_years(start, end):
      if start < 1500 or start > 2100:
      return
      if end < 1500 or end > 2100:
      return
      i = 0
      for i in range(start, end + 1):
      if i % 4 == 0 and (i % 100 != 0 or i % 400 == 0):
      print(i)


      print(leap_years(1998, 2008))


      I want to output be 3 and not 2000,2004,2008







      python python-3.x






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 9 at 21:04









      Daniel Mesejo

      8,2691923




      8,2691923










      asked Nov 9 at 21:03









      Martin

      82




      82






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          This is a good place to consider a list comprehension to create a list of leap years:



          def leap_years(start, end):
          if start < 1500 or start > 2100:
          return
          if end < 1500 or end > 2100:
          return

          leaps = [ i for i in range(start, end+1) if i % 4 == 0 and (i % 100 != 0 or i % 400 == 0) ]

          print( len(leaps) )

          leap_years(1998,2008)


          Result is:
          3






          share|improve this answer



























            up vote
            2
            down vote













            You need to add a counter:



            def leap_years(start, end):
            if start < 1500 or start > 2100:
            return 0
            if end < 1500 or end > 2100:
            return 0
            i, count = 0, 0
            for i in range(start, end + 1):
            if i % 4 == 0 and (i % 100 != 0 or i % 400 == 0):
            count += 1
            return count

            print(leap_years(1998, 2008))


            Output



            3


            In the above code the counter is the variable count each time the leap year condition is met it increments by 1. Note that now the function leap_years returns the amount of leap years.






            share|improve this answer





























              up vote
              2
              down vote













              We can use sum(...) here to count the number of elements:



              def leap_years(start, end):
              if not 1500 < start < 2100:
              return 0
              if not 1500 < end < 2100:
              return 0
              return sum(
              (not y % 4 and y % 100 != 0) or not y % 400
              for y in range(start, end + 1)
              )


              This works since in Python True is equal to 1, and False to 0, so we count the number of elements.



              The above is however not very efficient: we can in fact calculate the number of leap years over huge ranges, without having to iterate over them.



              We can for example calculate the number of elements dividably by 4 between a and b (both inclusive), by calculating ((b - c)/4) + 1 where c is the next element dividable by 4.



              With the same logic we can exclude the number of elements dividable by 100, and include the ones dividably by 400, like:



              def num_div(div, frm, to):
              return max(0, (to - (frm + (div - frm) % div) + div) // div)

              def leap_years(start, end):
              return num_div(4, start, end) - num_div(100, start, end) + num_div(400, start, end)


              For example, given the Gregorian calendar with these rules always existed, the number of leap years between 1234 AD and 123'456'789 AD is:



              >>> leap_years(1234, 123456789)
              29937972





              share|improve this answer






















                Your Answer






                StackExchange.ifUsing("editor", function ()
                StackExchange.using("externalEditor", function ()
                StackExchange.using("snippets", function ()
                StackExchange.snippets.init();
                );
                );
                , "code-snippets");

                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "1"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader:
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                ,
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                 

                draft saved


                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233238%2fcounting-output%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                0
                down vote



                accepted










                This is a good place to consider a list comprehension to create a list of leap years:



                def leap_years(start, end):
                if start < 1500 or start > 2100:
                return
                if end < 1500 or end > 2100:
                return

                leaps = [ i for i in range(start, end+1) if i % 4 == 0 and (i % 100 != 0 or i % 400 == 0) ]

                print( len(leaps) )

                leap_years(1998,2008)


                Result is:
                3






                share|improve this answer
























                  up vote
                  0
                  down vote



                  accepted










                  This is a good place to consider a list comprehension to create a list of leap years:



                  def leap_years(start, end):
                  if start < 1500 or start > 2100:
                  return
                  if end < 1500 or end > 2100:
                  return

                  leaps = [ i for i in range(start, end+1) if i % 4 == 0 and (i % 100 != 0 or i % 400 == 0) ]

                  print( len(leaps) )

                  leap_years(1998,2008)


                  Result is:
                  3






                  share|improve this answer






















                    up vote
                    0
                    down vote



                    accepted







                    up vote
                    0
                    down vote



                    accepted






                    This is a good place to consider a list comprehension to create a list of leap years:



                    def leap_years(start, end):
                    if start < 1500 or start > 2100:
                    return
                    if end < 1500 or end > 2100:
                    return

                    leaps = [ i for i in range(start, end+1) if i % 4 == 0 and (i % 100 != 0 or i % 400 == 0) ]

                    print( len(leaps) )

                    leap_years(1998,2008)


                    Result is:
                    3






                    share|improve this answer












                    This is a good place to consider a list comprehension to create a list of leap years:



                    def leap_years(start, end):
                    if start < 1500 or start > 2100:
                    return
                    if end < 1500 or end > 2100:
                    return

                    leaps = [ i for i in range(start, end+1) if i % 4 == 0 and (i % 100 != 0 or i % 400 == 0) ]

                    print( len(leaps) )

                    leap_years(1998,2008)


                    Result is:
                    3







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 9 at 21:47









                    seayak

                    6617




                    6617






















                        up vote
                        2
                        down vote













                        You need to add a counter:



                        def leap_years(start, end):
                        if start < 1500 or start > 2100:
                        return 0
                        if end < 1500 or end > 2100:
                        return 0
                        i, count = 0, 0
                        for i in range(start, end + 1):
                        if i % 4 == 0 and (i % 100 != 0 or i % 400 == 0):
                        count += 1
                        return count

                        print(leap_years(1998, 2008))


                        Output



                        3


                        In the above code the counter is the variable count each time the leap year condition is met it increments by 1. Note that now the function leap_years returns the amount of leap years.






                        share|improve this answer


























                          up vote
                          2
                          down vote













                          You need to add a counter:



                          def leap_years(start, end):
                          if start < 1500 or start > 2100:
                          return 0
                          if end < 1500 or end > 2100:
                          return 0
                          i, count = 0, 0
                          for i in range(start, end + 1):
                          if i % 4 == 0 and (i % 100 != 0 or i % 400 == 0):
                          count += 1
                          return count

                          print(leap_years(1998, 2008))


                          Output



                          3


                          In the above code the counter is the variable count each time the leap year condition is met it increments by 1. Note that now the function leap_years returns the amount of leap years.






                          share|improve this answer
























                            up vote
                            2
                            down vote










                            up vote
                            2
                            down vote









                            You need to add a counter:



                            def leap_years(start, end):
                            if start < 1500 or start > 2100:
                            return 0
                            if end < 1500 or end > 2100:
                            return 0
                            i, count = 0, 0
                            for i in range(start, end + 1):
                            if i % 4 == 0 and (i % 100 != 0 or i % 400 == 0):
                            count += 1
                            return count

                            print(leap_years(1998, 2008))


                            Output



                            3


                            In the above code the counter is the variable count each time the leap year condition is met it increments by 1. Note that now the function leap_years returns the amount of leap years.






                            share|improve this answer














                            You need to add a counter:



                            def leap_years(start, end):
                            if start < 1500 or start > 2100:
                            return 0
                            if end < 1500 or end > 2100:
                            return 0
                            i, count = 0, 0
                            for i in range(start, end + 1):
                            if i % 4 == 0 and (i % 100 != 0 or i % 400 == 0):
                            count += 1
                            return count

                            print(leap_years(1998, 2008))


                            Output



                            3


                            In the above code the counter is the variable count each time the leap year condition is met it increments by 1. Note that now the function leap_years returns the amount of leap years.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 9 at 21:16

























                            answered Nov 9 at 21:05









                            Daniel Mesejo

                            8,2691923




                            8,2691923




















                                up vote
                                2
                                down vote













                                We can use sum(...) here to count the number of elements:



                                def leap_years(start, end):
                                if not 1500 < start < 2100:
                                return 0
                                if not 1500 < end < 2100:
                                return 0
                                return sum(
                                (not y % 4 and y % 100 != 0) or not y % 400
                                for y in range(start, end + 1)
                                )


                                This works since in Python True is equal to 1, and False to 0, so we count the number of elements.



                                The above is however not very efficient: we can in fact calculate the number of leap years over huge ranges, without having to iterate over them.



                                We can for example calculate the number of elements dividably by 4 between a and b (both inclusive), by calculating ((b - c)/4) + 1 where c is the next element dividable by 4.



                                With the same logic we can exclude the number of elements dividable by 100, and include the ones dividably by 400, like:



                                def num_div(div, frm, to):
                                return max(0, (to - (frm + (div - frm) % div) + div) // div)

                                def leap_years(start, end):
                                return num_div(4, start, end) - num_div(100, start, end) + num_div(400, start, end)


                                For example, given the Gregorian calendar with these rules always existed, the number of leap years between 1234 AD and 123'456'789 AD is:



                                >>> leap_years(1234, 123456789)
                                29937972





                                share|improve this answer


























                                  up vote
                                  2
                                  down vote













                                  We can use sum(...) here to count the number of elements:



                                  def leap_years(start, end):
                                  if not 1500 < start < 2100:
                                  return 0
                                  if not 1500 < end < 2100:
                                  return 0
                                  return sum(
                                  (not y % 4 and y % 100 != 0) or not y % 400
                                  for y in range(start, end + 1)
                                  )


                                  This works since in Python True is equal to 1, and False to 0, so we count the number of elements.



                                  The above is however not very efficient: we can in fact calculate the number of leap years over huge ranges, without having to iterate over them.



                                  We can for example calculate the number of elements dividably by 4 between a and b (both inclusive), by calculating ((b - c)/4) + 1 where c is the next element dividable by 4.



                                  With the same logic we can exclude the number of elements dividable by 100, and include the ones dividably by 400, like:



                                  def num_div(div, frm, to):
                                  return max(0, (to - (frm + (div - frm) % div) + div) // div)

                                  def leap_years(start, end):
                                  return num_div(4, start, end) - num_div(100, start, end) + num_div(400, start, end)


                                  For example, given the Gregorian calendar with these rules always existed, the number of leap years between 1234 AD and 123'456'789 AD is:



                                  >>> leap_years(1234, 123456789)
                                  29937972





                                  share|improve this answer
























                                    up vote
                                    2
                                    down vote










                                    up vote
                                    2
                                    down vote









                                    We can use sum(...) here to count the number of elements:



                                    def leap_years(start, end):
                                    if not 1500 < start < 2100:
                                    return 0
                                    if not 1500 < end < 2100:
                                    return 0
                                    return sum(
                                    (not y % 4 and y % 100 != 0) or not y % 400
                                    for y in range(start, end + 1)
                                    )


                                    This works since in Python True is equal to 1, and False to 0, so we count the number of elements.



                                    The above is however not very efficient: we can in fact calculate the number of leap years over huge ranges, without having to iterate over them.



                                    We can for example calculate the number of elements dividably by 4 between a and b (both inclusive), by calculating ((b - c)/4) + 1 where c is the next element dividable by 4.



                                    With the same logic we can exclude the number of elements dividable by 100, and include the ones dividably by 400, like:



                                    def num_div(div, frm, to):
                                    return max(0, (to - (frm + (div - frm) % div) + div) // div)

                                    def leap_years(start, end):
                                    return num_div(4, start, end) - num_div(100, start, end) + num_div(400, start, end)


                                    For example, given the Gregorian calendar with these rules always existed, the number of leap years between 1234 AD and 123'456'789 AD is:



                                    >>> leap_years(1234, 123456789)
                                    29937972





                                    share|improve this answer














                                    We can use sum(...) here to count the number of elements:



                                    def leap_years(start, end):
                                    if not 1500 < start < 2100:
                                    return 0
                                    if not 1500 < end < 2100:
                                    return 0
                                    return sum(
                                    (not y % 4 and y % 100 != 0) or not y % 400
                                    for y in range(start, end + 1)
                                    )


                                    This works since in Python True is equal to 1, and False to 0, so we count the number of elements.



                                    The above is however not very efficient: we can in fact calculate the number of leap years over huge ranges, without having to iterate over them.



                                    We can for example calculate the number of elements dividably by 4 between a and b (both inclusive), by calculating ((b - c)/4) + 1 where c is the next element dividable by 4.



                                    With the same logic we can exclude the number of elements dividable by 100, and include the ones dividably by 400, like:



                                    def num_div(div, frm, to):
                                    return max(0, (to - (frm + (div - frm) % div) + div) // div)

                                    def leap_years(start, end):
                                    return num_div(4, start, end) - num_div(100, start, end) + num_div(400, start, end)


                                    For example, given the Gregorian calendar with these rules always existed, the number of leap years between 1234 AD and 123'456'789 AD is:



                                    >>> leap_years(1234, 123456789)
                                    29937972






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 9 at 21:29

























                                    answered Nov 9 at 21:14









                                    Willem Van Onsem

                                    140k16132225




                                    140k16132225



























                                         

                                        draft saved


                                        draft discarded















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233238%2fcounting-output%23new-answer', 'question_page');

                                        );

                                        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







                                        Popular posts from this blog

                                        Use pre created SQLite database for Android project in kotlin

                                        Darth Vader #20

                                        Ondo