Created
May 15, 2017 15:10
-
-
Save TApicella/86faec48f2d391407c9f267771b59fa5 to your computer and use it in GitHub Desktop.
Daily Programmer 5-15-2017- matrix transpose created by tapicella - https://repl.it/Hy9H/37
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Write a program that takes input text from standard input and outputs the text -- transposed. | |
Roughly explained, the transpose of a matrix | |
A B C | |
D E F | |
is given by | |
A D | |
B E | |
C F | |
Rows become columns and columns become rows. | |
philly | |
dev | |
becomes: | |
pd | |
he | |
iv | |
l | |
l | |
y | |
''' | |
def pprint(matrix): | |
for row in matrix: | |
print(' '.join(row)) | |
def process(val): | |
result = [] | |
if isinstance(val, str): | |
rowstring = val.split('\n') | |
if ' ' in val: | |
for r in rowstring: | |
result.append(r.split(' ')) | |
else: | |
for r in rowstring: | |
result.append(list(r)) | |
return result | |
def transpose(m): | |
result = [] | |
matrix = process(m) | |
for row in range(len(matrix)): | |
for i in range(len(matrix[row])): | |
while len(result) < len(matrix[row]): | |
result.append([]) | |
result[i].append(matrix[row][i]) | |
pprint(result) | |
print() | |
return(result) | |
test1 = [[1,2,3,4],[5,6]] | |
test2 = 'A B C\nD E F' | |
test3 = 'philly\ndev' | |
def userInput(): | |
new_input = input("Input your array. Submit a blank input to finish typing\n") | |
final_input = new_input | |
while new_input.strip() !='': | |
final_input+='\n' | |
new_input = input() | |
final_input+=new_input | |
return final_input | |
transpose(test1) | |
transpose(test2) | |
transpose(test3) | |
my_input = userInput() | |
transpose(my_input) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment