Chapter 1 Arrays and Strings - 1.6

Problem 1.6: Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?

The question is not clear. The interviewer should specify by which direction the matrix should be rotated (clockwise or counter-clockwise). I will rotate it clockwise by 90 degrees.

Do it by copying is quite easy:
def rotate_90degree(M, N):
    M_new = [[0 for j in range(0, N)] for i in range(0, N)]
    for i in range(0, N):
        for j in range(0, N):
            M_new[j][N-i-1] = M[i][j]
    return M_new

For the in place requirement, I came up with a smart solution quickly (by drawing some instances on the paper). In my solution, I divide a matrix into several circles (rectangles) and each circle can rotate independently. The rotation of a circle can be simplified to rotation of several pixels on this circle and the rotation of other pixels will be driven by rotation of these pixels. So I successfully reduce the problem to rotation of one pixel in place, which seems easy enough.

My solution is similar to the standard one. Dividing the problem into sub problems is a great strategy!
def rotate_90degree_inplace(M, N):
    last_circle = 0
    if N%2 == 0:
        last_circle = int(N/2)
    else:
        last_circle = int(N/2) - 1
    
    # From outermost to center, rotate each circle of elements
    for i in range(0, last_circle):
        rotate_circle(i, M, N)


def rotate_circle(i, M, N):
    # Rotate each element on the circle.
    # Only a part of the elements on the circle should be
    # rotated and the rotation of other elements will be driven by them
    for j in range(i, N-i-1):
        rotate_pixel(i, j, M, N)


def rotate_pixel(i, j, M, N):
    print "Rotate pixel: ", i, j
    # Note that the rotation of one element will
    # drive another 3 elements on the same circle to rotate.
    
    # Swap the four elements
    indices = []
    elements = []
    for k in range(0, 4):
        indices.append((i, j))
        elements.append(M[i][j])
        i, j = j, N-i-1
    M[indices[0][0]][indices[0][1]] = elements[3]
    for k in range(1, 4):
        M[indices[k][0]][indices[k][1]] = elements[k-1]

It took me more than one hour to implment rotate_pixel because it's not as easy as I thought before. Pay attention to
i, j = j, N-i-1
because it is different from
i = j
j = N-i-1
The later one is equivalent to
i = j
j = N-j-1
which caused a bug in my first version of implementation.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 类型错误:只有大小为1的数组 这个错误通常出现在使用numpy数组时,当你尝试使用一个大小不为1的数组作为参数传递给只接受大小为1的函数时,就会出现这个错误。解决方法是检查你的代码,确保你传递给函数的数组大小正确。 ### 回答2: Type Error: Only Size-1 Arrays 是一种常见的错误类型,通常会在使用 NumPy 数组时发生。该错误通常是由于尝试将未被分配的一个数组作为另一个数组的索引而引起的。 更具体地说,Type Error: Only Size-1 Arrays 往往会在以下情况下发生: 1. 在 NumPy 中,尝试使用一个未被分配的数组作为索引 2. 尝试使用错误的数据类型对数组进行操作 3. 在使用基于数组的函数或方法时出现类型不匹配的错误 解决这个错误的方法取决于引发它的原因。以下是一些常见的解决方法: 1. 确保在使用数组时分配了正确的维度,以便数组中的每个元素都具有正确的形状。可以使用 np.shape() 函数来检查数组的维度和形状。 2. 考虑使用不同的数据类型或转换数据类型以确保正确地执行操作。 3. 阅读 NumPy 文档并查找有关数组操作的详细信息,以确定如何正确地使用函数或方法。 最后,值得一提的是,使用代码编辑器和调试工具可以大大减少 Type Error: Only Size-1 Arrays 等错误的发生和发现。因此,始终在编写代码时保持准确性和细心是非常重要的。 ### 回答3: Type error: only size-1 arrays 是一个常见的 Python 错误。该错误通常会在 NumPy 库的使用过程中出现,而导致这个错误的原因可能是多方面的。 首先,这个错误通常是由于代码中未正确调用 NumPy 库中的函数所致。例如,在使用 NumPy 库中的函数时,如果传递给函数的参数不是一个有效的数组或矩阵,则会出现这个错误。 另一个导致这个错误的原因可能是使用了不兼容的数据类型。例如,在 NumPy 数组中只能使用同一种数据类型的元素,如果使用了不兼容的数据类型,则会出现这个错误。 最后,当尝试将不同大小的数组拼接在一起时,也可能会出现 Type error: only size-1 arrays 错误。 为了解决这个问题,用户可以采取一些措施。首先,必须确定每个 NumPy 函数的正确参数。其次,需要确保传递给函数的所有输入参数都是有效的数组,并且数据类型兼容。最后,如果尝试将两个不同大小的数组拼接起来,则必须确保在拼接之前将它们转换为相同的大小。 总之,Type error: only size-1 arrays 错误是由于 NumPy 库函数调用的问题或者输入不兼容数据类型所致。无论是哪种情况,都必须经过仔细的检查和修改才能够成功解决。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值