[英]Count occurrences of digit 'x' in range (0,n]
所以我正在尝试编写一个 python function,它接受两个 arguments,n 和 num,并计算 0 和 num 之间“n”的出现次数。 例如,
countOccurrences(15,5)
应该是2
。
countOccurrences(100,5)
应该是20
。
我对这个问题做了一个简单的迭代解决方案:
def countOccurrences(num,n):
count=0
for x in range(0,num+1):
count += countHelper(str(x),n)
return count
def countHelper(number,n):
count=0
for digit in number:
if digit==n:
count += 1
return count
如果我尝试调用countOccurrences(100000000000,5)
,这会遇到明显的问题。 我的问题是如何提高效率? 我希望能够“相当”快速地处理问题,并避免出现 memory 个错误。 这是我尝试执行此操作的递归解决方案的第一次传递:
def countOccurence(num, n):
if num[0]==n:
return 1
else:
if len(num) > 1:
return countOccurence(num[1:],n) + countOccurence(str((int(num)-1)),n)
else:
return 0
除非max_num
足够小以适合C long
, max_num
这不会遇到任何内存问题。 基本上,它仍然是蛮力算法,尽管已针对Python进行了优化。
def count_digit(max_num, digit):
str_digit = str(digit)
return sum(str(num).count(str_digit) for num in xrange(max_num+1))
我已经解决了我的解决方案,希望它符合您的规格。 让我们看一下第一个辅助函数:
def splitNumber(num):
temp = str(number)
nums = list(temp)
return nums
此函数创建一个字符串列表,列出所有数字输入中的各个数字。 例如,
splitNumber(100)
会返回:
['1', '0', '0']
在这里,您可以调用main函数并使用此main函数测试每个数字:
def countOccurences(num, n):
count = 0
for x in range(0, (num + 1)):
temp = splitNumber(x)
for x in range(len(temp)):
if (temp[x] == str(n)):
count = count + 1
return count
哪个应该给出期望的输出。 让我知道这是否适合您!
请参阅: https://math.stackexchange.com/a/1229292/974150
在 python 中:
def counts_of_i_bf(n, i):
"""Counts the number of occurences in a range [0 .. n] of
the digit i [0...9]
Args:
n ([int]): upper value of range [0 ... n]
i ([type]): digit looking for [0.. 9]
Returns:
[int]: occurences of i in the range [0...n]
"""
return sum(str(d).count(str(i)) for d in range(n + 1))
def counts_of_i_dp(n, i):
"""Counts the number of occurences in a range [1 .. n] of
the digit i [1...9] by implementing the recurrence
relation:
| ak.10^(k-1) + fi(b) if a < i
fi(a.10^k +b) = | ak.10^(k-1) + 1 + fi(b) + b if a == i
| (ak + 10).10^(k-1) + fi(b) if a > i
see: https://math.stackexchange.com/a/1229292/974150
Args:
n ([int]): upper value of range [1 ... n]
i ([type]): digit looking for [1.. 9]
Returns:
[int]: occurences of i in the range [0...n]
"""
som = 0
while n > 0:
k = int(log10(n))
a = n // 10**k
b = n - a * 10**k
if a < i:
som += a*k*10**(k-1)
elif a == i:
som += a*k*10**(k-1) + 1 + b
else:
som += (a*k + 10)*10**(k-1)
n = b
return int(som)
def counts_of_0(n):
"""Counts the number of occurences in a range [1 .. n] of
the digit0 by implementing:
Tn = (k + 1)*(b + 1 + (a - 1)10^k) + ∑ 9*s*10(s - 1) for s=1.. k\
f0(n) = Tn -∑ 9s.10^(s-1) for s=1..9
see: https://math.stackexchange.com/a/1229292/974150
Args:
n ([int]): upper value of range [1 ... n]
Returns:
[int]: occurences of 0 in the range [1...n]
"""
k = int(log10(n))
a = n // 10**k
b = n - a * 10**k
Tn = (k + 1)*(b + 1 + (a - 1)*10**k) + sum(9*s*10**(s - 1) for s in range(1, k + 1))
return Tn - sum(counts_of_i_dp(n, i) for i in range(1, 10)) + 1 # was one of
def counts_of_i(n, i):
"""Counts the number of occurences in a range [0 .. n] of
the digit i [0...9]
Args:
n ([int]): upper value of range [0 ... n]
i ([type]): digit looking for [0.. 9]
Returns:
[int]: occurences of i in the range [0...n]
"""
if n == 0: return 1 if i == 0 else 0
if i == 0: return counts_of_0(n)
return counts_of_i_dp(n, i)
assert all(counts_of_i_bf(i, d) == counts_of_i(i, d) for i in range(1_001) for d in range(10))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.