Lecture 2 Finger Exercise

The questions below are due on Friday May 08, 2026; 11:59:00 PM.
 
You are not logged in.

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 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.

Determine the index location of the first occurrence of char within text. Assign the variable answer to your final answer. For example:

  • When text = "abcdefffg" and char = "f", you should assign answer to 5.
  • When 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 **, 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.

Implement this strategy, assuming the variables 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.

For example:

  • When query = 16 and epsilon = 0.001, your code should assign answer something close to 4.
  • When query = 17 and epsilon = 0.001, your code should assign answer something close to 4.123.
  • When query = 200 and epsilon = 1e-5, your code should assign answer something close to 14.14213597.