LPTHW ex49 - Which mechanic is driving the loop?

Hello everyone!
I struggling to understand the mechanics of ex49.

According to the book I use the ‘skip’ function to filter out ALL! the ‘stop’ tuples that I don’t need to analyse. However, I simply don’t see how the code should iterate through an entire list? Does it not stop after the first element in the ‘word_list’?

  1. I call ‘skip’.
  2. As ‘word_list’ I pass something on like: [(‘noun’, ‘Martin’), (‘verb’, ‘set’), (‘stop’, ‘the’), (‘noun’, ‘house’), (‘stop’, ‘on’), (‘stop’, ‘fire’)].
  3. I set ‘word_type’ to ‘stop’.
  4. According to the book, the code should filter out all ‘stop’-tuples but I do not see how?
  5. ‘skip’ would run ‘peek’.
  6. ‘peek’ goes into the list and returns the first element of the first tuple (in this case ‘noun’).
  7. ‘skip’ would use the returned value from ‘peek’ (‘noun’) and compares it to the ‘word_type’ (‘stop’).
  8. As values ‘noun’ and ‘stop’ are not equal, the code stops and this was it?

Please let know were my thinking is wrong.
Many many thanks for all your help in advance.

Stephan

For convince, I paste in the code here again:

def peek(word_list):
    if word_list:
        word = word_list[0]
        return word[0]
    else:
        return None


def match(word_list, expecting):
    if word_list:
        word = word_list.pop(0)
        if word[0] == expecting:
            return word
        else:
            return None
    else:
        return None


def skip(word_list, word_type):
    while peek(word_list) == word_type:
        match(word_list, word_type)
1 Like

Yes, skip only throws away words until it hits one that doesn’t match the given type.

When you parse strings you usually have some kind of grammar where you handle tokens differently depending on what came last. Filtering out all tokens of a specific type is not something you need to do very often.

1 Like

Yes, that’s right. I think what you’re missing is that match() is destructive. See how it does this:

word = word_list.pop(0)

That removes the first element from the list, so it’s making the list of word tuples shrink. Try printing this out before and after so you can see it.

1 Like

Thanks so much florian! Please find my response below

Hi @florian florian! Hi @zedshaw!
Thanks so much for your response! Really much appreciated!

Ok, so you are saying it is designed to only check the first tuple for its type and only if this is of type ‘stop’, it kicks it out of the list. I see how pop would reduce the list. Afterwards it runs again and only if the second tuple is of type ‘stop’ again it would also kick this one out of the list.

The loop stops as soon as it finds a tuple of type ‘verb’ or ‘noun’ for example.
This would also mean that if the list contains any tuples of type ‘stop’ after the found tuple of type ‘verb’ (or ‘noun’), they would still remain in the list?

I think I simply got caught up by reading the below section of the book. I thought we try to get rid of all the ‘stop’ tuples in the list in this one step but instead we only want to delete them until we find a ‘verb’ or ‘noun’ (or any other ‘error’, ‘number’ tuple).

“Remember that skip doesn’t skip one word, it skips as many words of that type as it finds. This makes it so if someone types, ”scream at the bear” you get ”scream” and ”bear.””

Yes, think of pop like taking cars off a train from the front of the train.n The clarification is “removes all of the words of that type from the front of the list.”

2 Likes