
Title:
0247 Translation Tables

Description:

Now I'm going to show you how these translation tables work.

I'm going to define a variable called "table"

that's using the string.maketrans function, which makes a translation table,

and I'm going to tranlate from the characters 'ABC' to '123.'

I can give any number of characters here

the characters I want to replace and the ones I want to replace them with.

I should say that this is using the string module,

so somewhere we have to say import string before we start doing any of this.

You only have to do that import once, of course.

Now I'm going to define a formula f to be a simple formula A plus B equals C.

Then I'm going to call the translate method of the formula f and pass it this translation table.

That will evaluate to the string 1 plus 2 equals 3.

It has taken each of the elements in the table,

and they correspond A to 1, B to 2, C to 3,

substituted those into f and given me back a brand new string.

Now if I go ahead and evaluate f.translate of table,

which is 1 plus 2 equals 3, then that will give me the result True,

because 1 plus 2 is 3, and that's a legal Python expression.

Now what I want you to do is to define for me the function "valid,"

which takes a filledin formula like 1 plus 2 equals 3, filledin formula f,

and returns True or False. True if the formula is, in fact, valid.

If it represents a true equation like this.

And False if it represents an invalid equation like 1 plus 3 equals 3.

Or it should also return False if it represents a error like 1 divided by 0 equals 3.

That wouldn't return True or False, that would signal an error,

and I want you to handle that within the code for valid.

I'll give you a hint,

which is you should consider using a try statement.

Try, do something, and then you can say "except ZeroDivisionError" something.

What that does is it executes the main body in which you can test if evaluating

this expression f is true or not and return appropriately,

but if evaluating the expression f causes a zero division error,

then this clause will catch it, and then you can do the appropriate thing here.

You should also think about if there's anything else that can go wrong

within the execution of valid.

Here's my version of the solution.

I'm defining valid, takes filledin formula f, and it's going to return True.

The main part is if we evaluate f and if that's true, then we should return True,

but I also had to check for the zero division error

and even to be a little bit more sore here,

I ended up checking for arithmetic error, which is a super class of zero division error.

It covers a few additional things like overflow of really big numbers.

You didn't have to do that. I would've been fine to just catch the zero division error.

If there is such an error, then you should return False.

But I did one more thing here, and it looks kind of complicated.

I'm using a regular expression search, and let's look at exactly what's going on

in this confusing part of the clause here.