Saint Louis University |
Computer Science 1300/5001
|
Computer Science Department |
Note: All of these problems are for practice. You are freely encouraged to work together and you do not need to submit any of this work for a grade.
To further understanding of Python expressions and the precedence of operators, you will explore two styles of questions:
Given a Python expression, you are to draw an evaluation tree that properly captures the order of precedence of the operations when evaluating the expression.
Given an algebraic expression (using standard mathematical notations), you are to provide a valid Python expression that could be used to compute the expression.
When we use variables within the expressions, you can assume that they are well-defined in the Python workspace at the time of evaluation.
n / j + k
Answer:
a * b - c + d
Answer:
(a + b) / c*d
Answer:
k = 3 * (a + b / c)
Answer:
k = 3 * (a5 + b42 / c1)
Answer:
a + b - c * d / e ** f
Answer:
3 + 4 == 7 and 1 + 1 != 4
Answer:
A and not B or C and D
Answer:
A or not B and C or D
Answer:
not A or B and C or not D
Answer:
Note: although we have not (yet) introduced Python's sqrt function for calculating square roots, note that the square root is equivalent to raising an expression go the power 0.5.
For each of these, you can test the general formula by first making assignments to the variables involved, and then evaluating the general expression.
12bh
Some sample values:
b | h | result |
---|---|---|
4 | 10 | 20.0 |
10 | 4 | 20.0 |
3 | 5 | 7.5 |
Answer:
12h(b1+b2)
Some sample values:
b1 | b2 | h | result |
---|---|---|---|
4 | 6 | 10 | 50.0 |
6 | 10 | 4 | 32.0 |
0 | 6 | 10 | 30.0 |
Answer:
8x2−12x+5
Some sample values:
x | result |
---|---|
0 | 5 |
1 | 1 |
-1 | 25 |
Answer:
(a+b)(a−b)
Some sample values:
a | b | result |
---|---|---|
5 | 3 | 16 |
10 | 4 | 84 |
4 | 10 | -84 |
Answer:
43πr3
Some sample values:
x | result |
---|---|
1 | 4.1888 |
1.25 | 8.1812 |
4 | 268.0823 |
Answer:
3+126+326+526
For this finite fraction, the result should be 3.145238 (which is slightly bigger than π. If you want to extend the pattern with one more level of denominator, you're approximation will go below π at 3.13968. Add yet another level, the result will be 3.143206.
Answer:
√a2+b2
Some sample values:
a | b | result |
---|---|---|
3 | 4 | 5.0 |
12 | 5 | 13.0 |
5 | 3 | 5.83 |
Answer:
−b+√b2−4ac2a
Some sample values:
a | b | c | result |
---|---|---|---|
1 | 4 | 4 | -2.0 |
1 | 4 | -4 | 0.8284 |
4 | 1 | 4 | -0.125 + 0.992j |
Answer:
mc2√1−v2c2
Some sample values:
m | c | v | result |
---|---|---|---|
1 | 4 | 0 | 16.0 |
2 | 4 | 0 | 32.0 |
1 | 4 | 2 | 18.4752 |
1 | 299792458 | 0 | 8.98755e+16 |
1 | 299792458 | 1e6 | 8.9876+16 |
Answer:
s=a+b+c2and then the area is computed as
√s(s−a)(s−b)(s−c)
Some sample values:
a | b | c | result |
---|---|---|---|
3 | 4 | 5 | 6.0 |
13 | 5 | 12 | 30.0 |
10 | 10 | 10 | 43.30 |
Answer: