English subtitles

← A Gruesome Problem Solution - Programming Languages

Get Embed Code
2 Languages

Showing Revision 5 created 05/25/2016 by Udacity Robot.

  1. Here we have a gruesome problem. Although the title
  2. is a bit menacing, all we're really going to do is
  3. color words. Sounds like fun. So, we're going to use two
  4. colors to start out with, blue and green. Now, we
  5. say a word is blue if a lower case
  6. b occurs before a lower case t within the word.
  7. We say a word is green if a lower case
  8. g occurs after the occurrence of a lower case t.
  9. Within the word. So an example of a blue
  10. word would be wombat, because we have a lowercase
  11. b, occurring before lowercase t. As an example of
  12. a green word, we have racketeering, because g occurs after
  13. the current lowercase t. As one last example, we
  14. have the word abating, which is blue, because b occurs
  15. before t. And green because g occurs after t.
  16. So, yes, one word can be both blue and green.
  17. And, lastly, we just find a new color. I will
  18. call is grue. It may look like some purplish-pink. But,
  19. it's a brand new color that you've never seen before
  20. because I say so. I word is grue if it's
  21. blue or green. And it can be both blue and
  22. green and still be grue. So all three of the
  23. words I used here are grue. So those are our
  24. gruesome words and we've been asked to identify all the grue
  25. words within a given string. And we're going to
  26. use our regular expressions skills to do that. So what
  27. is a regular expression going to look like? My basic
  28. strategy is going to write it like this. Where I'm
  29. going to define what a blue word is like
  30. and a green word is like and match both words
  31. using the regular expression or operator. So first let's look
  32. at blue. Here I have the regex for blue words.
  33. We have zero more characters followed by a lower case b.
  34. And in between the B and the T can be zero
  35. more characters, and after the B and the T can be
  36. zero more characters. We're going to do the same thing with green. Here
  37. I have a T and a G with zero more characters
  38. before and after and in between. So, here I have a solution
  39. to the problem. I import the regular expression library, and then
  40. I just use re dot find all, with the completed regular expression.
  41. First we identify. Blue words and then afterwards we do the green words
  42. and we just feed in the string that we passed into the function.