Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I'm super new to Python and needing some direction for a school assignment.

I have a sample grid value:

grid = [['.', '.', '.', '.', '.', '.'],
        ['.', 'O', 'O', '.', '.', '.'],
        ['O', 'O', 'O', 'O', '.', '.'],
        ['O', 'O', 'O', 'O', 'O', '.'],
        ['.', 'O', 'O', 'O', 'O', 'O'],
        ['O', 'O', 'O', 'O', 'O', '.'],
        ['O', 'O', 'O', 'O', '.', '.'],
        ['.', 'O', 'O', '.', '.', '.'],
        ['.', '.', '.', '.', '.', '.']]

- - 
This is the output I need to achieve:

..OO.OO..
.OOOOOOO.
.OOOOOOO.
..OOOOO..
...OOO...
....O....

What I have tried:

in checking for some examples i have seen code such as:

<pre>for row in grid:
    for col in row:
        print(col, end='\t')

but its wrong for the desired output. i also saw some reference to using grid[0][0],  and looping from there but im still lost on how to properly use the loop in this case. any guidance is appreciated, ty
Posted
Updated 12-Feb-21 20:12pm

1 solution

Try this:
Python
grid = [['.', '.', '.', '.', '.', '.'],
        ['.', 'O', 'O', '.', '.', '.'],
        ['O', 'O', 'O', 'O', '.', '.'],
        ['O', 'O', 'O', 'O', 'O', '.'],
        ['.', 'O', 'O', 'O', 'O', 'O'],
        ['O', 'O', 'O', 'O', 'O', '.'],
        ['O', 'O', 'O', 'O', '.', '.'],
        ['.', 'O', 'O', '.', '.', '.'],
        ['.', '.', '.', '.', '.', '.']]
rotated = [list(reversed(x)) for x in zip(*grid)]
for row in rotated:
    for col in row:
        print(col, end='')
    print()
Now you only have one problem: finding out how it works before you hand it in! :D
 
Share this answer
 
Comments
YoungCoder72 13-Feb-21 23:34pm    
TVM OriginalGriff 🤙 I'll get working on the understanding the loops/code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900