Counting the distance between words in Python

Question

Suppose you are given two words, word_1 and word_2, as well as a string containing them, called your_string.

Given this, write a function using Python to find the minimum distance between word_1 and word_2. For the context of this question, distance is defined as the number of words between word_1 and word_2.
For example, given the following:

Input: your_string = 'the quick rabbit ran through the field with a carrot', word_1 = 'quick', word_2 = 'field'

Output: 4
#Since there are 4 words between 'quick' and 'field' in our string.

Solution

Access restricted

Subscribe to premium account to see the solution.

Get premium now