我正在尝试制作一个递归检查数字的脚本,例如检查两个字符串是否具有相同数量的某个数字。这就是我到目前为止所做的:
while(x<9):
n = str(x)
if a.count(n) == b.count(n):
cow=+1
x+=1
但是,这会重复并且不会返回它所在的函数。
答案 0 :(得分:0)
如果你想检查字符串中有多少位'x',你可以使用这个递归函数:
def recursive_count(input_str, x):
if len(input_str) == 0:
return 0
tot = 0
if input_str[0] == str(x):
tot += 1
tot += recursive_count(input_str[1:], x)
return tot
工作原理:
如果您想比较两个可以使用的字符串:
if recursive_count(string_1, x) == recursive_count(string_2, x):
foo = bar
进一步解释其工作原理的一个例子:
recursive_count('121', 1)
recursive_count('21', 1)
recursive_count('1', 1)
recursive_count('', 1)
recursive_count('', 1)
添加0并返回1 recursive_count('1', 1)
添加1并返回1 recursive_count('21', 1)
添加1并返回2 recursive_count('121', 1)
返回1 如果你想比较两个字符串是否经常有任何数字,你可以使用:
import numpy as np
def recursive_count(input_str):
tot = np.zeros(10)
if len(input_str) == 0:
return tot
tot[int(input_str[0])] += 1
tot += recursive_count(input_str[1:])
return tot
def compare_two(input_str_1, input_str_2):
first_res = recursive_count(input_str_1)
second_res = recursive_count(input_str_2)
if any(first_res == second_res):
print('Strings have at least one integer equally often*')
print('*This could also be 0 times')
如果某个数字的零都不计算,你可以做这样的事情(不是很好,但我现在可以想出最好的):
same_digits = [i for i in range(10) if first_res[i] == second_res[i] and first_res[i] != 0]
if len(same_digits) > 0 :
print('Strings have {} digits equally often'.format(same_digits))
答案 1 :(得分:0)
刚看到它必须是递归的。
这可以完成这项工作,但效率不高:
def compare(num_1, num_2):
if len(num_1) == 0 or len(num_2) == 0 : return 0
if num_1[0] == num_2[0]: return 1 + compare(num_1[1:], num_2[1:])
return max(compare(num_1,num_2[1:]), compare(num_1[1:],num_2))
答案 2 :(得分:0)
我建议使用通用count_chars
函数,使用添加
def count_chars (iter):
def loop (iter, answer = {}):
if not iter:
return list (answer.items ())
else:
char, *next = iter
answer[char] = answer[char] + 1 if char in answer else 1
return loop (next, answer)
return loop (iter, {})
print (count_chars ("abbcccdddd"))
# [('a', 1), ('b', 2), ('c', 3), ('d', 4)]
如您所见,count_chars
适用于任何可迭代,包括字符串。现在我们想要一种方法来获取两个单独的计数并区分它们。这是count_diff
def count_diff (c1, c2):
answer = dict (c1)
for (char, count) in c2:
answer[char] = answer[char] - count if char in answer else -count
return [ (char, count) for (char, count) in answer.items () if count != 0 ]
print (count_diff (count_chars ("ab"), count_chars ("bcd")))
# [('a', 1), ('c', -1), ('d', -1)]
print (count_diff (count_chars ("abcz"), count_chars ("bbcccdddd")))
# [('a', 1), ('b', -1), ('c', -2), ('z', 1), ('d', -4)]
在上面,我们可以看到当计数仅出现在左侧输入c1
时为正,而当它仅出现在负时正确输入c2
。换句话说,[('a', 1)]
的结果表示左输入比右输入多1
a
。 [('c', -2)]
的结果表示正确的输入比左输入多2
'c'
。当char在两个输入中均匀显示时,它不会出现在输出中。
如果输入相同,则返回空差异
str = "abbcccddd123"
print (count_diff (count_chars (str), count_chars (str)))
# []
使用它来区分4位字符串现在很容易。最重要的是,请注意字符串中数字的顺序无关紧要
def diff_numbers (str1, str2):
diff = count_diff (count_chars (str1), count_chars (str2))
return [ (int (char), count) for (char, count) in diff if char.isdigit () ]
print (diff_numbers ("abc123", "abc234"))
# [(1, 1), (4, -1)]
print (diff_numbers ("8888", "8878"))
# [(8, 1), (7, -1)]
print (diff_numbers ("1234", "4231"))
# []
因为这是一个学术问题,我将展示如何手工编写count_chars
。但如果这是一个真正的程序,使用像itertools.groupby
(下面)这样的东西可能会更好
from itertools import groupby
def count_chars (iter):
return [ (char, len (list (count))) for (char, count) in groupby (iter) ]
def count_diff (c1, c2):
answer = dict (c1)
for (char, count) in c2:
answer[char] = answer[char] - count if char in answer else -count
return [ (char, count) for (char, count) in answer.items () if count != 0 ]
def diff_numbers (str1, str2):
diff = count_diff (count_chars (str1), count_chars (str2))
return [ (int (char), count) for (char, count) in diff if char.isdigit () ]
print (diff_numbers ("abc123", "abc234"))
# [(1, 1), (4, -1)]
print (diff_numbers ("8888", "8878"))
# [(8, 1), (7, -1)]
print (diff_numbers ("1234", "4231"))
# []