# -------------------------------------------------------------- # Length of vector # -------------------------------------------------------------- def length(vector) : '''Assumes that vector is a tuple of floating point values. Returns the square root of the sum of the squares of the elements of vector. Hint: math.sqrt(25) is 5.0 For example, length((1, 1)) returns a value close to 1.414, since sqrt(1**2 + 1**2) = sqrt(2) = 1.414 length((-1.0, 2.0, 2.0, -1.0)) returns 3.1623, i.e., sqrt(1 + 4 + 4 + 1) = sqrt(10) ''' pass # -------------------------------------------------------------- # The Testing # -------------------------------------------------------------- class myTests(unittest.TestCase): def test1(self): self.assertAlmostEqual(length((1, 1)), 1.4142136, delta=0.0001) def test2(self): self.assertAlmostEqual(length((-1, 2, 2, -1)), 3.16228, delta=0.0001) def test3(self): self.assertAlmostEqual(length((0.5, 9.3, 0.8)), 9.34773, delta=0.0001) def test4(self): self.assertAlmostEqual(length((0,)), 0, delta=0.0001) def test5(self): self.assertAlmostEqual(length((50.2, 50.3)), 71.0643, delta=0.0001) if __name__ == '__main__': unittest.main(exit=True)
# --------------------------------------------------------------
# Length of vector
# --------------------------------------------------------------
def length(vector) :
'''Assumes that vector is a tuple of floating point values.
Returns the square root of the sum of the squares of the elements of vector.
Hint: math.sqrt(25) is 5.0
For example, length((1, 1)) returns a value close to 1.414,
since sqrt(1**2 + 1**2) = sqrt(2) = 1.414
length((-1.0, 2.0, 2.0, -1.0)) returns 3.1623, i.e., sqrt(1 + 4 + 4 + 1) = sqrt(10)
'''
pass
# --------------------------------------------------------------
# The Testing
# --------------------------------------------------------------
class myTests(unittest.TestCase):
def test1(self):
self.assertAlmostEqual(length((1, 1)), 1.4142136, delta=0.0001)
def test2(self):
self.assertAlmostEqual(length((-1, 2, 2, -1)), 3.16228, delta=0.0001)
def test3(self):
self.assertAlmostEqual(length((0.5, 9.3, 0.8)), 9.34773, delta=0.0001)
def test4(self):
self.assertAlmostEqual(length((0,)), 0, delta=0.0001)
def test5(self):
self.assertAlmostEqual(length((50.2, 50.3)), 71.0643, delta=0.0001)
if __name__ == '__main__':
unittest.main(exit=True)
Hi.
Let's move on to the code in the next step.
I have included comments in the code that will help you in understanding the code better.
Step by step
Solved in 2 steps with 2 images