24.0k views
2 votes
Write a function check_halves() that recursively processes the first half and second half of a string, counting how many letters the halves have in common, stopping when the first mismatch is found

User Pixartist
by
4.1k points

1 Answer

2 votes

Answer:

The program code is at explaination

Step-by-step explanation:

Below is the Python Function;

def check_halves(s):

# exact half not possible condition

if len(s)%2!=0:

return 0

halflen=len(s)//2

s1=s[:halflen]

s2=s[halflen:]

#null string condition

if len(s1)==0 or len(s2)==0:

return 0

#checking 1st digit of string recursively

if s1[0]==s2[0]:

return 1+check_halves(s1[1:]+s2[1:])

else:

return 0

You will find the input and output ad attachment.

Write a function check_halves() that recursively processes the first half and second-example-1
User Oscar Bralo
by
4.1k points