YouTube

Got a YouTube account?

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

English subtitles

← 03-07 Matchset Solution

dummy description

Get Embed Code
1 Language

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

  1. Here's my answer. A dot matches any character in the text.
  2. If the text has at least one character, then there's going to be a match,
  3. and the match is going to have a remainder, which is all the rest of the text.
  4. That remainder is a set,
  5. and it's the set of one element, and the element is the text from the first character on.
  6. In other words, dropping off the 0th character.
  7. We're going to return that if there is any text at all.
  8. That is, if the text is not empty.
  9. Otherwise, if the text is empty then we're going to return the null set.
  10. Defined the null set down here.
  11. How about for oneof?
  12. Oneof takes a string of possible characters and what it should return is similar to dot.
  13. It should return the remaining characters if the first character
  14. is equal to one of the characters in x.
  15. We're going to return exactly the same thing,
  16. a set consisting of a signal answer which is the original text
  17. with the first character dropped off, and we're going to return that.
  18. What I'd like to be able to say if the text starts with any of the characters in x.
  19. It turns out that I can actually say exactly that--text.startswith(x),
  20. if I arrange to have x be a tuple of characters rather than a character string.
  21. Here I have the documentation from the Python manual for the string that starts with method,
  22. and it says it's true if the string starts with a prefix,
  23. so we can ask does some string start with a particular string--yes or no?
  24. But prefix can also be a tuple of prefixes to look for.
  25. All we have to do is arrange for that x to be a tuple,
  26. and then it automatically searches for all of them.
  27. What I'm saying is I want the API function oneof('antibiotic').
  28. That should return some object, but we're not yet specifying what that object is,
  29. such that the operator of that object is oneof,
  30. and the x for that object should be the tuple a, b, c.
  31. It's sort of a little optimization here that I've defined the API to construct
  32. an object of just the right form so that I can use it efficiently here.
  33. Just go ahead and check does the text start with any one of the possible x's.
  34. Otherwise, return no.
  35. If you didn't know about this form of text.startswith,
  36. you could have just checked to see if any of the character c for c in x.
  37. We'd say return the rest of the string if any of hte characters in x
  38. if the text starts with any one of those characters.
  39. Otherwise, return "no."