Nearly two years ago, I set out on the quest for a new house. Well, not new by any stretch of the word "new", but more foreclosed and dilapidated. It was 2008 and the market was flooded with homes, especially those of the not quite livable variety. As I explored house after house after house, I became intimately familiar with a device known as a key lock box. Generally used by realtors, key lock boxes are a convenient, low-tech way to provide combination code access to a lock that would otherwise require a physical key for access.

This was actually the first time I had ever seen a key lock box. Essentially, they're small steel boxes that contain a key and are often affixed near a keyhole (eg., the door handle). Some feature a set of dials that must be aligned with the right code, while others (such as the one pictured) use a pushbutton system to enter the combination.

Being that they're such low-tech contraptions, the pushbutton key lock boxes don't quite work like you might expect. For example, one popular brand will unlock if the four-digit code is entered in sequence, regardless of what or how many digits preceded it. That is, if the code is 2-3-4-5, pressing 1-2-3-4-5 will open the box. Taking it a step further, the sequence 4-8-2-9-5-1-4-5 would cover the codes 4-8-2-9 8-2-9-5 9-5-1-4 and 5-1-4-5.

Bring Your Own Code

Your exercise for the day: write a function that outputs a sequence numbers which cover all combinations within the sequence.

  • The input should be:
    • Easy - nothing; just assume 4-digit combination code and 10 buttons
    • Hard - two variables: an integer that represents the combination code length, and the number of buttons on the lockbox
  • The output should be a valid sequence that covers all combinations

For bonus points, develop a variation wherein combination sequence doesn't matter; that is, pressing 1-2-3-4-5 not only covers 1-2-3-4 and 2-3-4-5, but also covers 1-2-4-3, 1-4-3-2, 1-4-2-3, and so on (but does not cover 1-2-3-5 and variants).

Thanks to Daniel Lundgren for suggesting this challenge.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!