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
python python-3.x
add a comment |
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
python python-3.x
add a comment |
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
python python-3.x
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
python python-3.x
edited Nov 9 at 21:04
Daniel Mesejo
8,2691923
8,2691923
asked Nov 9 at 21:03
Martin
82
82
add a comment |
add a comment |
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
add a comment |
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.
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 9 at 21:47
seayak
6617
6617
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 9 at 21:16
answered Nov 9 at 21:05
Daniel Mesejo
8,2691923
8,2691923
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Nov 9 at 21:29
answered Nov 9 at 21:14
Willem Van Onsem
140k16132225
140k16132225
add a comment |
add a comment |
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%2f53233238%2fcounting-output%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