Python Please **Bold is code** Recap of two-dimensional arrays and their numpy implementation import numpy as np my_2d_array = np.array([[1,2],[3,4],[5,6]]) print("This is my_2d_array:") print(my_2d_array) print("This array has shape " + str(my_2d_array.shape) + " as there are " + str(my_2d_array.shape[0]) + " rows and " + str(my_2d_array.shape[1]) + " columns.") print("The entry of the array with row index " + str(1) + " and column index " + str(1) + " has value " + str(my_2d_array[1,1])) 1) In this problem, implement a TwoDArray class that is meant to mimic (some of) the functionality of a two-dimensional numpy array. DO NOT use numpy at any point. 1A) Give the class an __init__ method Make sure that the variable array is a valid input. This means you should a) Make sure that array is a list of lists. and b) Make sure that each of the inner lists has the same length. You do not need to check for anything else. Rise the appropriate error(s) if these conditions are not met. It is up for you to decide if you should raise a TypeError, KeyError, IndexError, ValueError, ZeroDivisionError, etc. When raising the error, you should also give an informative error message.Assume that all numbers are ints or float,s to only worry about checking that the variable array is a list of lists. 1A) def __init__(self, array): self.array = array *** What would be the rest of the code
Python Please
**Bold is code**
Recap of two-dimensional arrays and their numpy implementation
import numpy as np
my_2d_array = np.array([[1,2],[3,4],[5,6]])
print("This is my_2d_array:")
print(my_2d_array)
print("This array has shape " + str(my_2d_array.shape) + " as there are " + str(my_2d_array.shape[0]) + " rows and " + str(my_2d_array.shape[1]) + " columns.")
print("The entry of the array with row index " + str(1) + " and column index " + str(1) + " has value " + str(my_2d_array[1,1]))
1) In this problem, implement a TwoDArray class that is meant to mimic (some of) the functionality of a two-dimensional numpy array. DO NOT use numpy at any point.
1A) Give the class an __init__ method
Make sure that the variable array is a valid input. This means you should
a) Make sure that array is a list of lists. and
b) Make sure that each of the inner lists has the same length. You do not need to check for anything else.
Rise the appropriate error(s) if these conditions are not met. It is up for you to decide if you should raise a TypeError, KeyError, IndexError, ValueError, ZeroDivisionError, etc. When raising the error, you should also give an informative error message.Assume that all numbers are ints or float,s to only worry about checking that the variable array is a list of lists.
1A)
def __init__(self, array):
self.array = array
*** What would be the rest of the code?
Algorithm:
- Start
- Create a class called TwoDArray with array as its attribute
- Implement constructor to initialize the data
- Implement checkType() method which checks whether the array is list of lists of not. if not, then raise an error with specific message else return true
- Implement checkLength() method which checks whether all the lists inside the list have same length or not. if not, raise an error with appropriate error message else return true
- Implement __repr__() method, that checks the validity of data by calling checkType() and checkLength() methods. If the array is valid, it returns the array else returns "Invalid Input"
- Inside the main method, create an object of TwoDArray and print the obejct
- Stop
Step by step
Solved in 4 steps with 2 images