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.
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.

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
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));
Step by step
Solved in 3 steps with 1 images









