[英]Finding adjacent pairs of characters in a string using recursion
I have to write a program that uses a recursive function to count the number of pairs of repeated characters in a string, and pairs of characters cannot overlap. 我必须编写一个使用递归函数来计算字符串中重复字符对的数量的程序,并且字符对不能重叠。
Say, for instance, I input the string "Hello, Sabeena". 举例来说,我输入了字符串“ Hello,Sabeena”。 I need the output to be "2", one for the pair of ls and one for the pair of es.
我需要输出为“ 2”,一个用于一对ls,一个用于一对es。
This is what I have tried, but there is no output. 这是我尝试过的,但是没有输出。
message = input("Enter a message:\n")
pairs = 0
k = 0
if len(message) == k:
return("Number of pairs:",pairs)
else:
if message[k] == message[k+1]:
pairs = pairs + 1
k = k+1
else:
k = k+1
Ideally, the program should look like this: 理想情况下,程序应如下所示:
Enter a message:
Hello, Sabeena
Number of pairs: 2
Can anybody suggest where I'm going wrong? 有人可以建议我要去哪里错吗?
Put the recursive code in a function (it can't really be considered recursive until it is), and return a recursive call to the function if you haven't reached your base case ( k == len(message) - 1
), incrementing k
each time. 将递归代码放入函数中(直到真正将其视为递归代码),如果尚未达到基本情况(
k == len(message) - 1
),则对函数返回递归调用,每次递增k
。
def find_adjacent(message, pairs, k):
if k == len(message) - 1: #subtract one to avoid getting a string index out of range error
return("Number of pairs:", pairs)
else:
return find_adjacent(message, pairs+1 if message[k]==message[k+1] else pairs, k+1)
if __name__ == "__main__":
message = "message" # change to input("Enter a message:\n")
pairs = 0
k = 0
m, p = find_adjacent(message, pairs, k)
print m, p
The above prints 以上印刷品
Number of pairs: 1
If you fancy it a bit less compact and quite a bit more readable: 如果您喜欢它,那么它的紧凑程度会降低,可读性会更高:
def find_adjacent(message, pairs, k):
if k == len(message)-1:
return("Number of pairs:", pairs)
elif message[k] == message[k+1]:
if k == 0:
return find_adjacent(message, pairs+1, k+1) # first letter of message, all good
elif message[k] != message[k-1]:
return find_adjacent(message, pairs+1, k+1) # not first letter, and this pair hasn't been counted before
else:
return find_adjacent(message, pairs, k+1) # this sequence has already been counted
else:
return find_adjacent(message, pairs, k+1)
Here's a recursive function that passes a new copy of the message, each time shorter. 这是一个递归函数,它每次都会传递一次新的消息副本。
This is what most recursive functions do: not memory-efficient but solving the problem in simpler ways than loop-based approaches. 这就是大多数递归函数的作用:不是高效的内存,而是比基于循环的方法以更简单的方式解决问题。 That's not the case here of course, but this is just an exercise.
当然,这里不是这种情况,但这只是一种练习。
def count_adj(message, adj, c):
"""Recursively counts the number of adjacent characters"""
if len(message) < 1:
return adj
else:
if c == message[0]:
adj += 1
return count_adj(message[1:], adj, message[0])
tests = ("Hello, Sabeeenaa", "Hi", "h", "HH", "HHH", "", " ", " ")
for t in tests:
print t, ":", count_adj(t, 0, '')
Results: 结果:
Hello, Sabeeenaa : 4
Hi : 0
h : 0
HH : 1
HHH : 2
: 0
: 0
: 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.