2 import numpy as np 3 4 my_2d_array np.array([[1,2],[3,4],[5,6]]) 5 print("This is my_2d_array:") 6 print(my_2d_array) 7 print("This array has shape str(my_2d_array.shape[0]) + + str(my_2d_array.shape) + as there are %3D rows and" + str(my_2d_array.shape[1]) + columns.") + str(1) + 8 print("The entry of the array with row index " " has value " and column index + str(1) + + str(my_2d_array[1,1]))

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Python Please

Recap of two-dimensional arrays and their numpy implementation 

** uploaded image ^

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.

If these conditions are not met you should raise the appropriate error(s). 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.

With actual numpy arrays, all numbers are required to be the same datatype. You don't have to worry about that here. You can also assume that all numbers are ints or floats so you only have to worry about checking that the variable array is a list of lists.

1B) Enable Scalar Multiplication

Make scalar multiplication work the same way as with numpy arrays (ex. print(2*my_2d_array)). For example, if x=TwoDArray([[1,2][3,4]]), then 2*x should be TwoDArray([[2,4][6,8]]). For full credit, you solution should be four lines or fewer (not including def). 

 

1A)

def __init__(self, array):

       self.array = array 

*** What goes here?

1B)

def __str__(self):
        return '[' + "\n ".join([str(arr) for arr in self.array]) + ']'
    
def shape(self):
        return (len(self.array), len(self.array[0]))

def __rmul__(self, c): 
          m, n = self.shape()

** What goes here?

2 import numpy as np
3
4 my_2d_array
np.array([[1,2],[3,4],[5,6]])
5 print("This is my_2d_array:")
6 print(my_2d_array)
7 print("This array has shape
str(my_2d_array.shape[0]) +
+ str(my_2d_array.shape) +
as there are
%3D
rows and"
+ str(my_2d_array.shape[1]) +
columns.")
+ str(1) +
8 print("The entry of the array with row index "
" has value "
and column index
+ str(1) +
+ str(my_2d_array[1,1]))
Transcribed Image Text:2 import numpy as np 3 4 my_2d_array np.array([[1,2],[3,4],[5,6]]) 5 print("This is my_2d_array:") 6 print(my_2d_array) 7 print("This array has shape str(my_2d_array.shape[0]) + + str(my_2d_array.shape) + as there are %3D rows and" + str(my_2d_array.shape[1]) + columns.") + str(1) + 8 print("The entry of the array with row index " " has value " and column index + str(1) + + str(my_2d_array[1,1]))
Expert Solution
Step 1

Program approach:

  • Starting a Python program to demonstrate the creation of a two-dimensional array.
  • Creating a class called TwoDArray.
  • Implementing checkType() method: checks whether the array is the list of lists or not.
  • Implementing the checkLength() method: checks whether all the lists inside the list have the same length or not.
  • Creating an object of TwoDArray inside the main method.
  • Printing the array.
  • Checking the validity of data whether the input is valid or not.
  • End of program.
steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY