The "outer" list data structure of 2-dimensional lists can be matrix-like ( having same number of elements in each "inner" list or "row"), or it can be ragged (having different number of elements in each "row"). a) Write a Python function called raggs that takes a single argument that is a reference to a 2-d list, and returns the value True if the 2-d list is ragged, and False otherwise. b) Demonstrate that the function works for all relevant cases by inputting different example values.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

The "outer" list data structure of 2-dimensional lists can be matrix-like ( having same number of elements in each "inner" list or "row"), or it can be ragged (having different number of elements in each "row").

a) Write a Python function called raggs that takes a single argument that is a reference to a 2-d list, and returns the value True if the 2-d list is ragged, and False otherwise.

b) Demonstrate that the function works for all relevant cases by inputting different example values.

Expert Solution
Introduction

A matrix is said to be ragged matrix if all the rows have different number of elements

We need to count the number of elements in each row. So, find the length of each row and whenever new length is encountered, it is ragged matrix and return True

Source Code

def ragged(list2D):         #declare function
    
    for l1 in list2D:       #for each row in 2d array
        if(len(l1) != len(list2D[0])):      #if length of any row is not same as length of first row
            return True;                    #it is ragged and return True
    return False;       #if all are of same dimenssion return False


#create 2d lists    
list1=[[0,0,0],[1,1,0],[2,2]];      #ragged
list2=[[0,0,0],[1,1,1],[2,2,1]];    #not ragged

#print their return values
print(ragged(list1));
print(ragged(list2));

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Lists
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education