Skip to content

Solution to |18 Oct 2022| Created by devzohaib #467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions 2022-Oct/18 Oct 2022/dev_zohaib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from typing import List


def get_input():
"""Get input from user and return as a list of lists.

>>> get_input()
Please enter the number of rows: 2
Please enter the number of columns: 3
Please enter the elements of the matrix in a single line and separated by a space: 1 2 3 4 5 6
[[1, 2, 3], [4, 5, 6]]
"""
rows = int(input('Please enter the number of rows: '))
columns = int(input('Please enter the number of columns: '))
print('Please enter the elements of the matrix in a single line and separated by a space: ', end='')
elements = list(map(int, input().split()))
return [elements[i:i + columns] for i in range(0, len(elements), columns)]


def transpose(matrix: List[List[int]]) -> List[List[int]]:
"""Transpose a matrix.

>>> transpose([[1, 2, 3], [4, 5, 6]])
[[1, 4], [2, 5], [3, 6]]
"""
trans_pose = [[matrix[i][j] for i in range(len(matrix))] for j in range(len(matrix[0]))]
return trans_pose


if __name__ == '__main__':
matrix = get_input()
matrix_transpose = transpose(matrix)

for row in matrix_transpose:
for row_entry in row:
print(row_entry, end=' ')
print()