YouTube

Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← 03-11 Search And Match Solution

dummy description

Get Embed Code
1 Language

Showing Revision 1 created 04/29/2012 by Amara Bot.

  1. Let's do match first.
  2. Match interfaces with the matchset, finds the set of remainders.
  3. If there is a set of remainders, then it finds the shortest one.
  4. The shortest remainder should be the longest text, and then we want to return that.
  5. We want to return the text, and it's a match not a search,
  6. so the match has to be at the beginning of the text, and it would go up to match.
  7. So we match from the beginning of the text. How far do we want to go?
  8. Well, everything except the remainder. How much is that?
  9. Well, we can just subtract from the length of the text the length of the shortest,
  10. and that gives us the initial piece of the longest possible match.
  11. Here search calls into match.
  12. What we do is we start iterating. We start at position number zero.
  13. If there is a match there for the text starting at position zero, then we want to return it.
  14. If not, we increment i to 1, and we say does the text from position 1 on--
  15. is there a match there and so on. We just keep on going through until we find one.
  16. Here what we want to say is "if the match is not None, then return m."
  17. Notice that it would be a bad idea to say "if m return m."
  18. Normally it's idiomatic in Python to say that if we're looking for a true value.
  19. But the problem here is that the empty string we want to count as a true value.
  20. A pattern might match the empty string. That counts as a match, not as a failure.
  21. We can't just say "if m" because the empty string counts as false.
  22. We have to say "if m is not None."