There is always another way: Sharpen your NumPy skills with the 8 Queens puzzle
summary
Walks through five concrete NumPy implementations to check diagonals for all 40,320 permutations (8!) and finds the 92 valid boards, comparing timings: initial diagonal-loop-with-trace ~3s, a strided loop ~12s, a broadcasting+fancy-indexing reshape ~1.5s, padded/stacked views ~1.10s, and a final np.bincount with weights solution at ~0.82s. Shows use of as_strides, broadcasting, fancy indexing, flips and padding to turn diagonals into rows, measures Python-to-NumPy call overhead, and confirms the fastest method both shortens code and wins on speed.
AI-written summary · about
chapters (17)
- 0:00Introduction and motivation
- 1:49About the speaker and context
- 2:50The 8 Queens puzzle explained
- 4:34First solution with NumPy trace
- 6:04Performance of first solution
- 6:27Understanding NumPy array structure
- 8:09Second solution with strided views
- 8:52Performance of second solution
- 9:14NumPy broadcasting and fancy indexing
- 10:37Third solution with diagonal rows
- 12:23Performance of third solution
- 13:05Fourth solution with zero padding
- 15:16Performance of fourth solution
- 16:00Fifth solution with bin count
- 18:11Performance of fifth solution
- 18:53Key takeaways and conclusions
- 20:36Final summary and Q&A
related talks
01h 27mMaking computation easier with cool Numpy tricks
34mNumPy: vectorize your brain
27mAPL-Inspired Techniques for Advanced NumPy
01h 31mMaking Computation Easier with Cool Numpy Tricks
35mThe "NumPy" Approach
01h 30mHow To Write a Rubik’s Cube Solver
38mVectorize all the things! Using linear algebra and NumPy to make your Python code lightning fast
32mPandas May Be Slow, but Pandas Doesn't Have to Be
