Open In App

Add custom borders to matrix in Python

Last Updated : 15 May, 2023
Comments
Improve
Suggest changes
1 Like
Like
Report

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.


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. 


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())


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.

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.

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.


Practice Tags :

Similar Reads