Skip to main content

Knight Dialer

This library shows how to solve the infamous knight dialer (KD) interview problem. There are many problems like it in interviews (see the staircase problem) but the basic premise is the same:

You have some start position and some end point, and at each point you can take one of a few possible steps. How many ways can you reach your end goal?

The Knight Dialer

The KD problem involves the standard phone keypad:

1 2 3
4 5 6
7 8 9
  0

How many possible phone numbers can you make by moving from one number to the next in the formation of the chess knight? So for instance, starting on 1, you can go to 6 or 8, and from 6 you have three choices: 1, 7, 0, but from 8 you have only two: back to 1 or to 3. As you can see, this is not a trivial programming problem you can just punch out in excel. Places have anywhere from 0 to 3 possible moves. You can try every possible permutation and then root out the non-knight move ones, but that probably starts to get prohibitively expensive, with 10! being over three million, that means that you’re starting to look at on the order of a billion operations to filter out. Probably that’s not the approach the interviewers want.

Before we get started though it’s worth just mentioning in this problem that you can drop 5 immediately because there’s no possible moves from there.

The Class of Problems

There are a number of classes of problems that behave like this one, and if you know how to solve this one, then you know how to solve most of them. They go like this, “given a starting point and a finish point, how many possible paths are there through the intervening space, given that at any point you have more than one choice you can make?”

Another example of such a problem given out at some point by google is “you have a staircase with 10 stairs. At each point you can either step 2 or 3 steps. How many possible paths are there to the top of the staircase?” Or in my library chemify, “can you write this sequence of letters in chemical elements given that they are either 1 or 2 characters in length?”

These problems are tricky and require some insight into how to solve them, but there is more than one way. I don’t ever see the multiple ways catalogued side by side, so herein lies the opportunity of this library.

Using the Library

To get the library:

pip install knight-dialer

The library contains three possible solutions to the problem. The first is a pure recursive solution. That is, the function calls itself down the number of dials, and then returns the answer back up. The second is a method that calls itself but doesn’t return the answer. Instead when it gets to the bottom it appends the finished work ot a list and returns that instead of itself. The final one is my personal recommendation for how to solve these kinds of problems in a no-stress way: a backtracking/todo system (as seen in my other library chemify). In this case, you add the initial items to a todo list, process each node, and then return the answer back to the todo list if it isn’t done yet, or to a ‘done’ list if it is. Once you have nothing on your todo list, return your done list.

IDES

If you want to get the best bang for your buck, look at the source code of this library with your IDE. You’ll find that the three functions, KD_pure_recursive, KD_recursive_addition, and KD_backtrack have documentation related to their overall structure and function, but if you dive into the source I’ve also annotated it line by line so you can see the thinking.

If you run the source file and have numpy and matplotlib installed it will run a speed test. Usually it seems that pure recursion wins out on my machine. These will probably be similar for problems like the knight dialer, but you may see more substantive differences for problems of greater depth or more branching.

In conclusion

Enjoy the library and let me know if you have questions! Thanks!

Metadata

Release files for knight-dialer 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for knight-dialer 0.0.2
File Size Uploaded Provenance
knight_dialer-0.0.2.tar.gz 6.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for knight-dialer 0.0.2
File Interpreter ABI Platform
knight_dialer-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 24.7 kB

Release files / knight_dialer-0.0.2.tar.gz

Download URL knight_dialer-0.0.2.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
94b89794c06c2a1ca067e3560a683e293743ef97e75cc85d8c897bad332b0f29
BLAKE2b-256 checksum
How to use checksums
08435e5d01d6db84c369a1eaa4f64a183300f5d741f04f082fb26aa0a39880c1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

Release files / knight_dialer-0.0.2-py3-none-any.whl

Download URL knight_dialer-0.0.2-py3-none-any.whl
Size 18.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e354b74aeaaa7c60b3374961f706cf6ae1478304e3cc228e21fe017622774e5a
BLAKE2b-256 checksum
How to use checksums
b681dbd6bce4d399e2279bb4623db6ed020d9df1cfe621c1b11154f9ecd94419
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

Security for knight-dialer 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page