66.4k views
5 votes
Write a recursive function same_char_same_pos() that takes two strings as parameters. It returns the number of characters which are the same, in the same positions, in both strings. For example, same_char_same_pos("cat","bat") should return 2. For example, same_char_same_pos("cat","acat") should return 0 because, although some of the letters are the same, they are not in the same positions in the two words. Turn this in to the GradeScope assignment "Exam 2 - Same Char Same Pos." Your code should be in a file named exam2_same_char_same_pos.py.

1 Answer

2 votes

Answer:

Check the explanation

Step-by-step explanation:

def same_char_same_pos(s1,s2):

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

return 0

else:

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

return 1+same_char_same_pos(s1[1:],s2[1:])

else:

return same_char_same_pos(s1[1:], s2[1:])

# Testing

print(same_char_same_pos("cat","bat"))

print(same_char_same_pos("cat","acat"))

Kindly check the output in the image below:

Write a recursive function same_char_same_pos() that takes two strings as parameters-example-1
User Mark Biek
by
4.6k points