Restructure Newton's method (Case Study: Approximating Square Roots) by decomposing it into three cooperating functions: newton, limitReached, and improveEstimate.
The newton function can use either the recursive strategy of Project 2 or the iterative strategy of the Approximating Square Roots Case Study. The task of testing for the limit is assigned to a function named limitReached, whereas the task of computing a new approximation is assigned to a function named improveEstimate. Each function expects the relevant arguments and returns an appropriate value.
An example of the program input and output is shown below:
Enter a positive number or enter/return to quit: 2
The program's estimate is 1.4142135623746899
Python's estimate is 1.4142135623730951
Enter a positive number
0.00
out of
10.00
The program's newton, limitReached, and improvedEstimate functions work as expected.
0 out of 3 checks passed. Review the results below for more details.
Checks
Unit TestIncomplete
The newton function returns the square root of 'x'
Test Output
Traceback (most recent call last):
File "/root/sandboxee2e1795/ ", line 2, in
from newton import *
File "/root/sandboxee2e1795/ ", line 10
val = improvateEstimate(val,n) if limitReached(val,last):
^
IndentationError: unindent does not match any outer indentation level
Test Contents
class UnitTests(unittest.TestCase):
def test_unit_test_1(self):
self.assertEqual(newton(2), 1.4142135623746899)
def test_unit_test_2(self):
self.assertEqual(newton(13), 3.6055513629176015)
Unit TestIncomplete
The limitReached function returns True if the estimate is within the tolerance, if not, False
Test Output
Traceback (most recent call last):
File "/root/sandboxee2e1795/ ", line 2, in
from newton import *
File "/root/sandboxee2e1795/ ", line 10
val = improvateEstimate(val,n) if limitReached(val,last):
^
IndentationError: unindent does not match any outer indentation level
Test Contents
class UnitTests(unittest.TestCase):
def test_unit_test_1(self):
ans = limitReached(2,1)
if ans != False:
ans = limitReached(1,2)
if ans != False:
raise AssertionError(f"Incorrect return value. Expected `False`, was {ans}")
def test_unit_test_2(self):
ans = limitReached(16,1)
if ans != False:
ans = limitReached(1,16)
if ans != False:
raise AssertionError(f"Incorrect return value. Expected `False`, was {ans}")
def test_unit_test_3(self):
self.assertEqual(limitReached(1,1), True)
Unit TestIncomplete
The improveEstimate function returns an improved estimate
Test Output
Traceback (most recent call last):
File "/root/sandboxee2e1795/ ", line 2, in
from newton import *
File "/root/sandboxee2e1795/ ", line 10
val = improvateEstimate(val,n) if limitReached(val,last):
^
IndentationError: unindent does not match any outer indentation level
Test Contents
class UnitTests(unittest.TestCase):
sys.tracebacklimit = 0
def test_unit_test_1(self):
ans = improveEstimate(2,1)
if ans != 1.5:
ans = improveEstimate(1, 2)
if ans != 1.5:
raise AssertionError(f"Incorrect return value: {ans} != 1.5")
def test_unit_test_2(self):
ans = improveEstimate(9,1)
if ans != 5.0:
ans = improveEstimate(1, 9)
if ans != 5.0:
raise AssertionError(f"Incorrect return value: {ans} != 5.0")