I want to find the number of zeroes included from 1 to 1000 by making a loop. The code is mentioned below.
var = 0
for n in range(1, 1001):
for "0" in n:
var += 1
print(var)
The output which I should get is 192 but it is showing an error
for "0" in n:
^
SyntaxError: can't assign to literal
Therefore, I would like to know from the community where am I going wrong.
THANK YOU
You probably want to do something like
var = 0
for n in range(0, 1001):
for digit in str(n):
if digit == '0':
var += 1
print(var)
the idea being that by turning the int
into a str
, you can iterate over each of the digits just like you can iterate over the letters in a word.
A more Pythonic approach to the same problem would be to simply do
sum(d == '0' for n in range(1001) for d in str(n))
or the shorter version
sum(str(n).count('0') for n in range(1001))
or the code-golfy
''.join(map(str, range(1001))).count('0')
A different approach would be the functional-ish
from collections import Counter
from operator import itemgetter
sum(map(itemgetter('0'), map(Counter, map(str, range(1001)))))
or sort of along the same lines
from operator import reduce
reduce(lambda x, y: x + y.count('0'), map(str, range(1001)), 0)
I agree with the comments above : you should use str(n) to cast the integer 0 into the character "O" that you are searching for.
One other way to do is to use the numeric value and not the character:
var = 0
for n in range(0, 1001):
for digit in n:
if digit == 0: #numerical value
var += 1
print(var)
Your range(0,1001) should start a 1, otherwise you'll be counting an extra zero. Going through all numbers and converting to a string works but it is very inefficient. You can obtain the number of zero using a recursive function that will go exponentially faster:
# String based version
def strZeroCount(N):
z = 0
for n in range(1,N+1):
z += str(n).count("0")
return z
# numerical version (recursive)
def numZeroCount(N):
if N < 10 : return 0
R = N % 10 + 1
Q = N // 10
return Q + numZeroCount(Q-1)*(10-R) + numZeroCount(Q)*R
Performance of the string version will be roughly O(n) while the recursive function will be closer to O(log n):
numZeroCount(1000) 0.006 ms (120x faster)
strZeroCount(1000) 0.727 ms
numZeroCount(1000000) 0.031 ms (12,000x faster)
strZeroCount(1000000) 374.3 ms
numZeroCount(1000000000) 0.180 ms (1,700,000x faster)
strZeroCount(1000000000) 310550 ms = 5.2 minutes
They way numZeroCount()
works is by separating the problem in two parts:
For example, if you take number 1037:
The recursive function can be further improved using a cache. This provides an acceleration because many of the recursions will compute the zero counts for the same prefixes:
from functools import lru_cache
@lru_cache(maxsize=100)
def numZeroCount(N):
if N < 10 : return 0
R = N % 10 + 1
Q = N // 10
return Q + numZeroCount(Q-1)*(10-R) + numZeroCount(Q)*R
numZeroCount(1000) 0.002 ms vs 0.006 ms
numZeroCount(1000000) 0.018 ms vs 0.031 ms
numZeroCount(1000000000) 0.020 ms vs 0.180 ms
numZeroCount(1000000000000) 0.021 ms vs 1.113 ms
numZeroCount(1000000000000000) 0.024 ms vs 8.099 ms
或使用外壳:
seq 0 1001 | grep '0' | tr -c -d '0' | wc -c
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.