Can someone please explain whats going on in this code
Can comments be added please?
class twoDarray(list):
def __init__(self, array):
try:
if(not isinstance(array[0],list)):
raise TypeError
x = len(array[0])
for i in array:
if len(i)!=x:
raise KeyError
except TypeError:
print("error Not a 2d array.")
return
except KeyError:
print("error Lists are not of equal size.")
return
self.array = array
def __str__(self):
return '[' + "\n ".join([str(arr) for arr in self.array]) + ']'
def shape(self):
return (len(self.array), len(self.array[0]))
The given code mimics the functionality of numpy array by creating a separate class named twoDarray.
The given code checks the validity of two dimensional matrix .
A two dimensional matrix is valid,
- if it is a list of lists
- If the length of all lists in the list is same
If the two dimensional matrix is valid, it prints the two dimensional matrix else prints appropriate error message.
The shape() method prints the dimensions of two dimensional matrix.
Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 2 images
- Step 1: Read your files! You should now have 3 files. Read each of these files, in the order listed below. staticarray.h -- contains the class definition for StaticArray, which makes arrays behave a little more like Python lists O In particular, pay attention to the private variables. Note that the array has been defined with a MAX capacity but that is not the same as having MAX elements. Which variable indicates the actual number of elements in the array? staticarray.cpp -- contains function definitions for StaticArray. Right now, that's just the print() member function. 0 Pay attention to the print() function. How many elements is it printing? main.cpp -- client code to test your staticarray class. Note that the multi-line comment format (/* ... */) has been used to comment out everything in main below the first print statement. As you proceed through the lab, you will need to move the starting comment (/*) to a different location. Other than that, no changes ever need to be made to…arrow_forward1Code a JavaScript callback function for the Array.map method to ('Yu Yamaguchi') evaluate an array of integersidentify the integers that are Prime numbersreturn the Prime numbers in the new array 2 Code a JavaScript callback function for the Array.reduce method to evaluate an array of integersdetermine the smalles integerreturn the smalles integer Note: if there are identical integers, the function is to return the last one.arrow_forwardMethod Details getCountInRange public static int getCountInRange(int[] array, int lower, int upper) Returns the number of values in the range defined by lower (inclusive) and upper (inclusive) Parameters: array - integer array lower - lower limit upper - upper limit Returns: Number of values found Throws: java.lang.IllegalArgumentException - if array is null or lower is greater than upper Any error message is fine (e.g., "Invalid parameters(s)")arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY