Add custom borders to matrix in Python
Last Updated :
15 May, 2023
Given a Matrix, the task is to write a python program to print each row having custom borders.
Input : test_list = [[4, 5, 6], [1, 4, 5], [6, 9, 1], [0, 3 ,1]], bord = "|"
Output : | 4 5 6 |
| 1 4 5 |
| 6 9 1 |
| 0 3 1 |
Explanation : Matrix is ended using | border as required.
Input : test_list = [[4, 5, 6], [1, 4, 5], [6, 9, 1], [0, 3 ,1]], bord = "!"
Output : ! 4 5 6 !
! 1 4 5 !
! 6 9 1 !
! 0 3 1 !
Explanation : Matrix is ended using ! border as required.
Method 1: Using loop
In this, we perform the task of printing the row elements using the inner loop, separated by space. The main step of adding custom borders is concatenated using the + operator.
Python3
test_list = [[ 4 , 5 , 6 ], [ 1 , 4 , 5 ],
[ 6 , 9 , 1 ], [ 0 , 3 , 1 ]]
print ( "The original list is : " + str (test_list))
bord = "|"
for sub in test_list:
temp = bord + " "
for ele in sub:
temp = temp + str (ele) + " "
temp = temp + bord
print (temp)
|
Output
The original list is : [[4, 5, 6], [1, 4, 5], [6, 9, 1], [0, 3, 1]]
| 4 5 6 |
| 1 4 5 |
| 6 9 1 |
| 0 3 1 |
Time Complexity: O(n2)
Auxiliary Space: O(1)
Method #2 : Using * operator + loop
In this, the task of joining inner characters is performed using * operator.
Python3
test_list = [[ 4 , 5 , 6 ], [ 1 , 4 , 5 ],
[ 6 , 9 , 1 ], [ 0 , 3 , 1 ]]
print ( "The original list is : " + str (test_list))
bord = "|"
for sub in test_list:
print (bord, * sub, bord)
|
Output
The original list is : [[4, 5, 6], [1, 4, 5], [6, 9, 1], [0, 3, 1]]
| 4 5 6 |
| 1 4 5 |
| 6 9 1 |
| 0 3 1 |
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #3: Using lists and its methods(insert(),append())
Python3
test_list = [[ 4 , 5 , 6 ], [ 1 , 4 , 5 ],
[ 6 , 9 , 1 ], [ 0 , 3 , 1 ]]
print ( "The original list is : " + str (test_list))
bord = "|"
res = []
for i in test_list:
x = list ( map ( str , i))
x.insert( 0 , bord)
x.append(bord)
print ( " " .join(x))
|
Output
The original list is : [[4, 5, 6], [1, 4, 5], [6, 9, 1], [0, 3, 1]]
| 4 5 6 |
| 1 4 5 |
| 6 9 1 |
| 0 3 1 |
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 4: Using list comprehension
Step-by-step approach:
- Initialize the border character board.
- Define a list comprehension that iterates over each sublist in test_list and concatenates the border character bord to the beginning and end of each sublist.
- Store the result in a new list res.
- Print the elements of res using a loop.
Python3
test_list = [[ 4 , 5 , 6 ], [ 1 , 4 , 5 ],
[ 6 , 9 , 1 ], [ 0 , 3 , 1 ]]
bord = "|"
res = [bord + " " .join( map ( str , lst)) + bord for lst in test_list]
for r in res:
print (r)
|
Output
|4 5 6|
|1 4 5|
|6 9 1|
|0 3 1|
Time complexity: O(nm), where n is the number of sublists in test_list and m is the maximum length of a sublist.
Auxiliary space: O(nm), where n is the number of sublists in test_list and m is the maximum length of a sublist.
Method 5: Using the string concatenation and multiplication operations.
Step-by-step approach:
- Iterate through each row of the matrix using a for loop.
- Concatenate the border character “|” to the beginning and end of the row using string concatenation operation.
- Use a nested for loop to iterate through each element of the row.
- Convert the element to a string using the str() function and concatenate a space character ” ” to it.
- Multiply the resulting string by the number of columns in the matrix minus 2 to fill the space between the borders.
- Concatenate the resulting string to the row using string concatenation operation.
- Append the resulting row to the list of results.
- Print the results.
Python3
test_list = [[ 4 , 5 , 6 ], [ 1 , 4 , 5 ],
[ 6 , 9 , 1 ], [ 0 , 3 , 1 ]]
bord = "|"
rows = len (test_list)
cols = len (test_list[ 0 ])
res = []
for row in test_list:
row_str = bord
for elem in row:
row_str + = str (elem) + " " * (cols - 2 ) + bord
res.append(row_str)
for r in res:
print (r)
|
Output
|4 |5 |6 |
|1 |4 |5 |
|6 |9 |1 |
|0 |3 |1 |
Time complexity: O(rows * cols), where rows and cols are the number of rows and columns in the matrix, respectively. We iterate through each element of the matrix once to construct the resulting strings.
Auxiliary space: O(rows), where rows is the number of rows in the matrix. We store the resulting strings in a list of length equal to the number of rows in the matrix.
Share your thoughts in the comments
Please Login to comment...