Skip to content
Advertisement

Recursively searching for a string in a list of characters

I have a problem to solve which is to recursively search for a string in a list (length of string and list is atleast 2) and return it’s positions. for example: if we had ab with the list ['a','b','c'], the function should return '(0,2)', as ab starts at index 0 and ends at 1 (we add one more).
if we had bc with the same list the function should return '(1,3)'.
if we had ac with the same list the function should return not found.

Note that I’m solving a bigger problem which is to recursively search for a string in a matrix of characters (that appears from up to down, or left to right only), but I am nowhere near the solution, so I’m starting by searching for a word in a row of a matrix on a given index (as for searching for a word in a normal list), so my code might have char_mat[idx], treat it as a normal list like ['c','d','e'] for example.
Note that my code is full of bugs and it doesn’t work, so I explained what I tried to do under it.

def search_at_idx(search_word, char_mat, idx, start, end):
if len(char_mat[idx]) == 2:
    if ''.join(char_mat[idx]) == search_word:
        return 0,2
    else:
        return 'not found', 'not found'
start, end = search_at_idx(search_word, char_mat[idx][1:], idx, start+1, end)
return start, end 

The idea of what I tried to do here is to find the base of the recursion (when the length of the list reaches 2), and with that little problem I just check if my word is equal to the chars when joined together as a string, and return the position of the string if it’s equal else return not found
Then for the recursion step, I send the list without the first character, and my start index +1, so if this function does all the job for me (as the recursion hypothesis), I need to check the last element in the list so my recursion works. (but I don’t know really if this is the way to do it since the last index can be not in the word, so I got stuck).

Now I know that I made alot of mistakes and I’m nowhere near the correct answer,I would really appreciate any explanation or help in order to understand how to do this problem and move on to my bigger problem which is finding the string in a matrix of chars.

Advertisement

Answer

I’ve thrown together a little example that should get you a few steps ahead

char_mat = [['c', 'e', 'l', 'k', 'v'],]
search_word = 'lk'

def search_at_idx(search_word, char_mat, idx, start=0):
    if len(char_mat[idx]) < len(search_word):
        return 'not', 'found'
    if ''.join(char_mat[idx][:len(search_word)]) == search_word:
        return start, start+len(search_word)
    char_mat[idx] = char_mat[idx][1:]
    start, end = search_at_idx(search_word, char_mat, idx, start+1)
    return start, end 

print(search_at_idx(search_word, char_mat, 0))

To point out a few errors of yours:

  • In your recursion, you use char_mat[idx][1:]. This will pass a slice of the list and not the modified matrix. That means your next call to char_mat[idx] will check the letter at that index in the array. I’ll recommend using the debugger and stepping through the program to check the contents of your variables

  • Instead of using start and end, you can always assume that the found word has the same length as the word you are searching for. So the distance you have to look is always start + len(search_word)

If you have any additional questions about my code, please comment.

Here’s an example for list comprehension if that counts as loophole:

foundword = list(map("".join, list(zip(*([char_mat[idx][i:] + list(char_mat[idx][i-1]) for i in range(len(search_word))])))[:-1])).index(search_word)
print((foundword, foundword + len(search_word)) if foundword else 'Not found')
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement