O'Reilly logo

Learn Python Programming by Fabrizio Romano

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementing a sudoku-solver in Python

Let's now explore my refactored implementation of the solver. I'm going to present the code to you in steps, as it is quite involved (also, I won't repeat the source name at the top of each snippet, until I move to another module):

# sudoku/algo/solver.pyimport osfrom itertools import zip_longest, chainfrom time import timedef cross_product(v1, v2):    return [w1 + w2 for w1 in v1 for w2 in v2]def chunk(iterable, n, fillvalue=None):    args = [iter(iterable)] * n    return zip_longest(*args, fillvalue=fillvalue)

We start with some imports, and then we define a couple of useful functions: cross_product and chunk. They do exactly what the names hint at. The first one returns the cross-product between two iterables, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required