Python Forum
what am I doing wrong?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what am I doing wrong?
#1
I have this assignment for my second class where I have to write a code that would rotate a matrix by 90* clockwise. I came up with the idea to iterate from the back on the sublists and create new sublists but the code doesn't work. Could somebody look at it and tell me what's wrong?
l=[[1,2,3],[4,5,6],[7,8,9]]

def rotate_matrix(l):
    if len(l) < 4:
        print("list too short")
    else:
        result = []
        for i in range(len(l) -1, -1, -1):
            row = []
            for a in i:
                row.append(a[i])
                result.append(row)
            return result
            print result
Reply
#2
Please put your code in Python code tags, you can find help here.
Explain better what about the code doesn't work. If you get an error message, post full error traceback message (in error tags). If result is incorrect, show the actual and desired result.
Reply
#3
l=[[1,2,3],[4,5,6],[7,8,9]]

def rotate_matrix(l):
if len(l) < 4:
print("list too short")
else:
result = []
for i in range(len(l) -1, -1, -1):
row = []
for a in i:
row.append(a[i])
result.append(row)
return result
print result

I have this assignment for my second class where I have to write a code that would rotate a matrix by 90* clockwise. I came up with the idea to iterate from the back on the sublists and create new sublists but the code doesn't work. Could somebody look at it and tell me what's wrong?

l=[[1,2,3],[4,5,6],[7,8,9]]

def rotate_matrix(l):
    if len(l) < 4:
        print("list too short")
    else:
        result = []
        for i in range(len(l) -1, -1, -1):
            row = []
            for a in i:
                row.append(a[i])
                result.append(row)
            return result
            print result
and nothing is printed
the expected output matrix is
result=[[7,4,1],[8,5,2],[9,6,3]]
Reply
#4
(Nov-08-2018, 05:57 PM)imamy Wrote: and nothing is printed
I would expect "list too short" to be printed, but that's only if you actually called the function. If you've shared your entire code, nothing happens because you never call the function.


Aside from that, the indentation looks off for more than half of the function.
Reply
#5
I see the first mistake - do you know how to implement len() of sublists?

how about now? could you give me any tips?

def rotate_matrix(l):
    result = []
    for m in range(len(l)-1, len(l)-1, -1):
        row = []
        for a in m:
            row.append(a[m])
        result.append(row)
    return result
    print result
            
            
l=[[1,2,3],[4,5,6],[7,8,9]]
rotate_matrix(l)
Reply
#6
Line 9 will not do anything. Because it occurs after the return and there is no alternate path through the function to reach it, print() will never be called. Also, if you're using Python 3, print() is not called correctly on that line; you need parentheses. In any case, you don't need the print() call there at all because you have the return. Really, you should have the print on line 13:

print(rotate_matrix(l))
Line 3, the range() shouldn't work. The results of both len() calls will be the same. Instead, you need to slice the matrix to get the length of one of its lists only. Since they're all the same length, there won't be any issues. You definitely want a range() call here, just not as written.

Line 5, "a in m" has no meaning because "m" is a number. Based on the intent of your code, you want to iterate over the lists inside the matrix at this point.

I recommend not using "l" as a variable name; lowercase "l" is simply too easy to confuse with the number 1.
Reply
#7
my idea was:
  • to iterate by elements of the lists (which are here sublists) from the last one to the first one (range (start, stop, step) )
  • from each of them to take the first element and add it to new [row]
  • add them all to a previously introduced list of sublists (new matrix)
Do I have to slice the list in order to do that?

(Nov-10-2018, 11:04 PM)imamy Wrote: The results of both len() calls will be the same.
what do you mean by that? I meant the two arguments of range function to be the start and stop conditions

(Nov-10-2018, 11:04 PM)imamy Wrote: Line 5, "a in m" has no meaning because "m" is a number.
By writing a in m or (a[m]) I wanted to get to an element of a sublist
Reply
#8
Do not iterate over range. Iterate over the list, or in this case reversed(matrix). I used matrix instead of l because you should avoid single letter variable names. They make your code hard to read, and that makes it harder for us to help you.

Start with a list of empty lists (output = [[], [], []]). For each sub-list, use enumerate to loop over it (for index, item in enumerate(sub_list)). Then append those items to the correct sub-list in the output (output[index].append(item)).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
ok I did it in a different way.
After doing everything I reversed the order of element on row

def rotate_matrix(matrix):
    result = []
    for i in range(len(matrix)):
        row=[]
        for j in matrix:
            row.append(j[i])
            for e in range(len(row) / 2):
                r_row = list(reversed(row))
        result.append(r_row)
    return result
            
            
matrix=[[1,2,3],[4,5,6],[7,8,9]]
print rotate_matrix(matrix)
this way I received my beautiful [[7,4,1],[8,5,2],[9,6,3]] for my input

THANK YOU ALL FOR HELP!
Reply
#10
(Nov-11-2018, 05:22 PM)imamy Wrote:
    for i in range(len(matrix)):
        row=[]
        for j in matrix:
            row.append(j[i])
I bet if you test it with a different matrix, where the size of the sublist is different from the size of the container list, you'll get an error.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020