Lecture 2 Finger Exercise
Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
Question 1.
Assume the variables Determine the index location of the first occurrence of text and char are accessible to you, each bound to a separate str.
All the characters comprising text are in lexicographic order, and len(char) == 1.char within text.
Assign the variable answer to your final answer.
For example:
text = "abcdefffg" and char = "f", you should assign answer to 5.text = "a" * 100 + "m" * 100 + "n" * 100 + "z" * 100 and char = "n", you should assign answer to 200.
Question 2.
Consider the problem of computing the square root of a (positive) number.
While we could simply use Python's exponentiation operator Implement this strategy, assuming the variables For example:**, let's consider an alternate strategy that more explicitly spells out the computation.
The following "pseudo-code" explains a series of steps one could take to approximate the square root.
It has been known since at least the year AD 60.Given a input query number:
1. Make an initial guess that the square root is is half the query. (This is most likely wrong.)
2. Multiply the guess by itself and see if it's close enough (within some epsilon) of the query.
3. If so, congratulations!
4. If not, update the guess as the average of guess and (query / guess).
5. Return to step 2.
query and epsilon are accessible to you, with each bound to a positive number (int or float).
Assign the variable answer to your final answer.
query = 16 and epsilon = 0.001, your code should assign answer something close to 4.query = 17 and epsilon = 0.001, your code should assign answer something close to 4.123.query = 200 and epsilon = 1e-5, your code should assign answer something close to 14.14213597.