Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Construct a class named Vec that will represent vectors of any size. A vector ob

ID: 3547697 • Letter: C

Question

Construct a class named Vec that will represent vectors of any size.  A vector object will be maintained as a list of numeric values.  The constructor should take a single (optional) argument specifying the initial length of the vector.  If no argument is provided, the vector should be initialized to zero length (Null vector).  If the initial vector length is greater than zero, all elements in the vector should be initialized to zero.  Provide two methods: Construct a class named Vec that will represent vectors of any size.  A vector object will be maintained as a list of numeric values.  The constructor should take a single (optional) argument specifying the initial length of the vector.  If no argument is provided, the vector should be initialized to zero length (Null vector).  If the initial vector length is greater than zero, all elements in the vector should be initialized to zero.  Provide two methods:

Explanation / Answer

class Vec:

def __init__(self,length=0):

self.length = length

self.list = length*[0]

def __str__(self):

return str(self.list)

def __len__(self):

return self.length

a = Vec()

b = Vec(5)

print(a)

print(len(a))

print(b)

print(len(b))

#if you have any doubt then comment on this answer and give indentation before running code