Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
NumPy Cookbook

You're reading from   NumPy Cookbook If you're a Python developer with basic NumPy skills, the 70+ recipes in this brilliant cookbook will boost your skills in no time. Learn to raise productivity levels and code faster and cleaner with the open source mathematical library.

Arrow left icon
Product type Paperback
Published in Oct 2012
Publisher Packt
ISBN-13 9781849518925
Length 226 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Toc

Table of Contents (17) Chapters Close

NumPy Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Winding Along with IPython 2. Advanced Indexing and Array Concepts FREE CHAPTER 3. Get to Grips with Commonly Used Functions 4. Connecting NumPy with the Rest of the World 5. Audio and Image Processing 6. Special Arrays and Universal Functions 7. Profiling and Debugging 8. Quality Assurance 9. Speed Up Code with Cython 10. Fun with Scikits Index

Stride tricks for Sudoku


The ndarray class has a strides field, which is a tuple indicating the number of bytes to step in each dimension when going through an array. Let's apply some stride tricks to the problem of splitting a Sudoku puzzle to the 3 by 3 squares of which it is composed.

Note

Explaining the Sudoku rules is outside the scope of this book. For more information see http://en.wikipedia.org/wiki/Sudoku.

How to do it...

  1. Define the Sudoku puzzle array

    Let's define the Sudoku puzzle array. This one is filled with the contents of an actual, solved Sudoku puzzle:

    sudoku = numpy.array([
        [2, 8, 7, 1, 6, 5, 9, 4, 3],
        [9, 5, 4, 7, 3, 2, 1, 6, 8],
        [6, 1, 3, 8, 4, 9, 7, 5, 2],
        [8, 7, 9, 6, 5, 1, 2, 3, 4],
        [4, 2, 1, 3, 9, 8, 6, 7, 5],
        [3, 6, 5, 4, 2, 7, 8, 9, 1],
        [1, 9, 8, 5, 7, 3, 4, 2, 6],
        [5, 4, 2, 9, 1, 6, 3, 8, 7],
        [7, 3, 6, 2, 8, 4, 5, 1, 9]
        ])
  2. Calculate the strides. The itemsize field of ndarray gives us the number of bytes in an array. Using the...

You have been reading a chapter from
NumPy Cookbook
Published in: Oct 2012
Publisher: Packt
ISBN-13: 9781849518925
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime