English subtitles

← 02-47 Translation Tables

Get Embed Code
1 Language

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

  1. Now I'm going to show you how these translation tables work.
  2. I'm going to define a variable called "table"
  3. that's using the string.maketrans function, which makes a translation table,
  4. and I'm going to tranlate from the characters 'ABC' to '123.'
  5. I can give any number of characters here--
  6. the characters I want to replace and the ones I want to replace them with.
  7. I should say that this is using the string module,
  8. so somewhere we have to say import string before we start doing any of this.
  9. You only have to do that import once, of course.
  10. Now I'm going to define a formula f to be a simple formula A plus B equals C.
  11. Then I'm going to call the translate method of the formula f and pass it this translation table.
  12. That will evaluate to the string 1 plus 2 equals 3.
  13. It has taken each of the elements in the table,
  14. and they correspond A to 1, B to 2, C to 3,
  15. substituted those into f and given me back a brand new string.
  16. Now if I go ahead and evaluate f.translate of table,
  17. which is 1 plus 2 equals 3, then that will give me the result True,
  18. because 1 plus 2 is 3, and that's a legal Python expression.
  19. Now what I want you to do is to define for me the function "valid,"
  20. which takes a filled-in formula like 1 plus 2 equals 3, filled-in formula f,
  21. and returns True or False. True if the formula is, in fact, valid.
  22. If it represents a true equation like this.
  23. And False if it represents an invalid equation like 1 plus 3 equals 3.
  24. Or it should also return False if it represents a error like 1 divided by 0 equals 3.
  25. That wouldn't return True or False, that would signal an error,
  26. and I want you to handle that within the code for valid.
  27. I'll give you a hint,
  28. which is you should consider using a try statement.
  29. Try, do something, and then you can say "except ZeroDivisionError" something.
  30. What that does is it executes the main body in which you can test if evaluating
  31. this expression f is true or not and return appropriately,
  32. but if evaluating the expression f causes a zero division error,
  33. then this clause will catch it, and then you can do the appropriate thing here.
  34. You should also think about if there's anything else that can go wrong
  35. within the execution of valid.
  36. Here's my version of the solution.
  37. I'm defining valid, takes filled-in formula f, and it's going to return True.
  38. The main part is if we evaluate f and if that's true, then we should return True,
  39. but I also had to check for the zero division error
  40. and even to be a little bit more sore here,
  41. I ended up checking for arithmetic error, which is a super class of zero division error.
  42. It covers a few additional things like overflow of really big numbers.
  43. You didn't have to do that. I would've been fine to just catch the zero division error.
  44. If there is such an error, then you should return False.
  45. But I did one more thing here, and it looks kind of complicated.
  46. I'm using a regular expression search, and let's look at exactly what's going on
  47. in this confusing part of the clause here.