avatarSantal Tech

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1427

Abstract

</span>, ]</span></pre></div><div id="4151"><pre>rotated = <span class="hljs-comment">[ <span class="hljs-comment">[7,4,1]</span>, <span class="hljs-comment">[8,5,2]</span>, <span class="hljs-comment">[9,6,3]</span>, ]</span> assert_equal(rotated, rotate_90_clockwise(original))</pre></div><p id="c85e">I initially learned of a nice method where you reverse the given matrix (row-wise), then switch the x and y-coordinates.</p><p id="3bcf">So something like this:</p><figure id="3c98"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*MBO5kZvkIag4TgDM8US6rQ.png"><figcaption></figcaption></figure><p id="b212">But then, my mind was blown from learning of a method like this:</p><figure id="030c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Dyl7TGMcfCJfqfwjn7qOfQ.png"><figcaption></figcaption></figure><p id="db30">Beautiful, right? Let’s take a look at how it works:</p><div id="4640"><pre>list(<span class="hljs-name">zip</span>(<span class="hljs-name">*m</span>[:<span class="hljs-symbol">:-1</span>]))</pre></div><p id="eceb">Working from the inside out, we have the following operations:</p><p id="3763"><code>m[::-1]</code> This reverses the original 2D array, row-wise. If we’re given <code>[[1,2,3],[4,5,6],[7,8,9]]</code> , this gives us <code>[[7,8,9],[4,5,6],[1,2,3]]</code>.</p><p id="fd9a">Then, the asterisk <code>*m</code> unpacks the array. This means we’re calling <c

Options

ode>zip</code> on <code>[7,8,9],[4,5,6],[1,2,3]</code> , not <code>[[7,8,9],[4,5,6],[1,2,3]]</code> (observe the extra opening and end bracket in the latter).</p><p id="83e5">Then, <code>zip</code> will take one element from each of the arrays and use that new array as part of its output until there are no more elements. For example:</p><ul><li>First iteration: take 7, 4, and 1. Row is [7,4,1].</li><li>Second iteration: take 8, 5, and 2. Row is [8,5,2].</li><li>Third iteration: take 9, 6, and 3. Row is [9,6,3].</li></ul><p id="f1d7">Then there aren’t any more elements to pull from!</p><p id="c314">Finally, we need a <code>list</code> because <code>zip</code> returns an iterator in Python3.</p><p id="59d6">The output of this function is<code>[[7,4,1],[8,5,2],[9,6,3]]</code> , which is exactly the answer we want.</p><p id="60ec">Hope you also thought this one-liner was cool!</p><p id="6521"><i>For more articles like this, <a href="https://medium.com/@SantalTech">follow me</a> on Medium. Not a member yet? <a href="https://medium.com/@SantalTech/membership">Join the community.</a></i> <i>Want more software engineering interview guides and coding question tips? Check out all of my writing organized by topic <a href="https://readmedium.com/santals-writing-index-a179642e3e31">in this article.</a></i></p><p id="188e"><i>If you have any requests for what I should write, please let me know!</i></p></article></body>

A clever one-liner to rotate 2D Arrays in Python

Here’s a fun question: say you want to rotate an array 90 degrees clockwise, like so:

1 2 3
4 5 6
7 8 9

becomes

7 4 1
8 5 2
9 6 3

How would you do it?

https://unsplash.com/photos/R401qwThw7w

In psuedo-code, this would look something like:

original = [
  [1,2,3],
  [4,5,6],
  [7,8,9],
]
rotated = [
  [7,4,1],
  [8,5,2],
  [9,6,3],
]
assert_equal(rotated, rotate_90_clockwise(original))

I initially learned of a nice method where you reverse the given matrix (row-wise), then switch the x and y-coordinates.

So something like this:

But then, my mind was blown from learning of a method like this:

Beautiful, right? Let’s take a look at how it works:

list(zip(*m[::-1]))

Working from the inside out, we have the following operations:

m[::-1] This reverses the original 2D array, row-wise. If we’re given [[1,2,3],[4,5,6],[7,8,9]] , this gives us [[7,8,9],[4,5,6],[1,2,3]].

Then, the asterisk *m unpacks the array. This means we’re calling zip on [7,8,9],[4,5,6],[1,2,3] , not [[7,8,9],[4,5,6],[1,2,3]] (observe the extra opening and end bracket in the latter).

Then, zip will take one element from each of the arrays and use that new array as part of its output until there are no more elements. For example:

  • First iteration: take 7, 4, and 1. Row is [7,4,1].
  • Second iteration: take 8, 5, and 2. Row is [8,5,2].
  • Third iteration: take 9, 6, and 3. Row is [9,6,3].

Then there aren’t any more elements to pull from!

Finally, we need a list because zip returns an iterator in Python3.

The output of this function is[[7,4,1],[8,5,2],[9,6,3]] , which is exactly the answer we want.

Hope you also thought this one-liner was cool!

For more articles like this, follow me on Medium. Not a member yet? Join the community. Want more software engineering interview guides and coding question tips? Check out all of my writing organized by topic in this article.

If you have any requests for what I should write, please let me know!

Python
Leetcode
Coding
Learning To Code
Software Engineering
Recommended from ReadMedium