K K
K K

Reputation: 41

Functions for searching through strings

enter image description hereI need some help to write 2 functions for a program. It should return true/false value if the characters in str(word) are also in the str(string).
The results should look like this:

find_indices("esimerkki", "esmerki")
    (True, [0, 1, 3, 4, 5, 6, 8])

find_indices("iiiieeee", "ieeiiee")
    (False, [0, 4, 5])

find_indices("iiiieeee", "iieee")
    (True, [0, 1, 4, 5, 6])

How to proceed? The second funtion should be easy as long as the first one is complete, but I don't know how to start the first one.

     def find_indices(string, word):
            """
            returns the first indices in 'string' that form (in order) the substring 'word'
            
            returns True, list of indices 
            - if all of the letters in 'word' were found in order
            
            returns False, list of indices as far as matches were found 
            - if none or only some of the letters in 'word' were found in order
            
            e.g.
            
            > find_indices("esimerkki", "sei")
            > (True, [1, 4, 8])
            
            > find_indices("esimerkki", "side")
            > (False, [1, 2])
            """
            # implement your code here!
        
        def find_indices_ignore_case(string, word):
            """
            finds the indices of 'word' in 'string in order - exactly like in 'find_indices', 
            except the case of the letters is ignored
            """
            # implement your code here!
            
        def main():
            # examples to test your functions
            example_cases = ( \
                ("esimerkki", ""), 
                ("esimerkki", "sei"), 
                ("esimerkki", "kis"), 
                ("iiiieeee", "iieee"), 
                ("iiiieeee", "iei"), 
                ("iiiieeee", "ieeiiee"), 
                ("esimerkki", "esmerki"), 
                ("Lorem ipsum dolor sit amet", "ohjelmointi"), 
                ("Lorem ipsum dolor sit amet, consectetur adipiscing elit", "on ohjelmointi on 
                 kivaa"), 
                ("Lorem ipsum dolor sit amet, consectetur adipiscing elit", "meri")
                )
            for example_string, example_word in example_cases:
                print("find_indices('{:s}', '{:s}') \nreturned {}\n".format(example_string, 
                example_word, find_indices(example_string, example_word)))
            
            print('find_indices("EsIMERKKITEKSTI", "SeiTi") \nreturned', 
                  find_indices("EsIMERKKITEKSTI", "SeiTi"))
            print('\nfind_indices_ignore_case("EsIMERKKITEKSTI", "SeiTi") \nreturned', 
                  find_indices_ignore_case("EsIMERKKITEKSTI", "SeiTi"))
            
        main()

Upvotes: 1

Views: 43

Answers (1)

user2390182
user2390182

Reputation: 73470

There is an approach:

def find_indices(string, word):
    iter_s = enumerate(string)
    indices = []
    for c1 in word:
        ind = next((i for i, c2 in iter_s if c1 == c2), None)
        if ind is None:            
            return False, indices
        indices.append(ind)
    return True, indices

>>> find_indices("esimerkki", "esmerki")
(True, [0, 1, 3, 4, 5, 6, 8])
>>> find_indices("esimerkki", "side")
(False, [1, 2])
>>> find_indices("esimerkki", "")
(True, [])
>>> find_indices("esimerkki", "sei")
(True, [1, 4, 8])
>>> find_indices("esimerkki", "kis")
(False, [6, 8])

iter_s is an iterator over the (index, character) pairs in string. This lazy iterator can only move forward. As you iterate through the word (for c1 in word), you move iter_s ahead until you hit a match (ind = next((... for i, c2 in iter_s ...), default)). If no such match is found (if ind is None), you can immediately return the negative result with the indeces you found until that point.

Upvotes: 1

Related Questions