Fork me on GitHub

Project Notes

#480 minMoves

Using python to unlock a backpack: cassidoo’s interview question of the week (2026-09-06).

Notes

The interview question of the week (2026-09-06):

You have a backpack lock’s starting position, and the code to unlock it, represented as two strings of integers. In one move, you may rotate any single digit one step up or down, with 0 and 9 considered adjacent. Return the minimum number of moves needed to transform the starting code into the unlock code.

Example:

minMoves("8051", "1199")
> 10

minMoves("000", "555")
> 15

minMoves("109", "990")
> 4

Thinking about the Problem

This seems pretty straight-forward. For each digit, we find the delta between the positions by simple subtraction. If the difference is more than 5, then we subtract from 10 i.e. rotate in reverse.

Initial Solution

The python zip() function makes it easy to iterate each pair of digits in turn. Then we can sum over a simple list comprehension:

def minMoves(current, code):
    return sum(
      10 - abs(int(a) - int(b)) if abs(int(a) - int(b)) > 5 else abs(int(a) - int(b))
      for a, b in zip(current, code)
    )

I think I prefer this as more canonical python to the (perhaps clearer) procedural version:

def minMoves(current, code):
    total = 0

    for a, b in zip(current, code):
        diff = abs(int(a) - int(b))
        distance = min(diff, 10 - diff)
        total += distance

    return total

And that works:

$ ./challenge.py 8051 1199
# Given:
# * Starting position: 8051
# * Lock Code: 1199
# Minimum moves required to unlock:
10
$ ./challenge.py 000 555
# Given:
# * Starting position: 000
# * Lock Code: 555
# Minimum moves required to unlock:
15
$ ./challenge.py 109 990
# Given:
# * Starting position: 109
# * Lock Code: 990
# Minimum moves required to unlock:
4

Improving the Code?

While it’s a clever one-liner, the duplicated difference calculation makes me itch.

We can nest the list comprehension to create a temporary variable diff:

def minMoves(current, code):
    return sum(
        (10 - diff if diff > 5 else diff)
        for a, b in zip(current, code)
        for diff in [abs(int(a) - int(b))]
    )

Yes, still works. Is it an improvement? Hmm, debatable!

Rather than do the explicit conditional (10 - diff if diff > 5 else diff), we could alternatively just calculate the min():

def minMoves(current, code):
    return sum(
        min(diff, 10 - diff)
        for a, b in zip(current, code)
        for diff in [abs(int(a) - int(b))]
    )

Tests

I’ve setup some validation in test_challenge.py:

$ ./test_challenge.py
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK

Final Code

Final code is in challenge.py:

#! /usr/bin/env python
from sys import argv
from sys import stderr


def minMoves(current, code):
    return sum(
        min(diff, 10 - diff)
        for a, b in zip(current, code)
        for diff in [abs(int(a) - int(b))]
    )


if __name__ == '__main__':
    if len(argv) == 3:
        current = argv[1]
        code = argv[2]
        print("# Given:", file=stderr)
        print("# * Starting position:", current, file=stderr)
        print("# * Lock Code:", code, file=stderr)
        print("# Minimum moves required to unlock:", file=stderr)
        print(minMoves(current, code))
    else:
        print("Usage: challenge.py '<current-position>' '<code>'", file=stderr)

Credits and References

About LCK#480
pythoncassidoo

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

Project Source on GitHub Return to the LittleCodingKata Catalog
About LittleCodingKata

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.

Follow the Blog follow projects and notes as they are published in your favourite feed reader