I am trying to generate all possible keypad sequences (7 digit length only right now). For example if the mobile keypad looks like this:
1 2 3
4 5 6
7 8 9
0
Some of the possible sequences can be:
123698
147896
125698
789632
The requirement is that the each digit of number should be neighbor of previous digit.
Here is how I am planning to start this:
The information about the neighbor changes from keypad to keypad so we have to hardcode it like this:
neighbors = {0: 8, 1: [2,4], 2: [1,3,5], 3: [2,6], 4: [1,5,7], 5: [2,4,6,8], 6: [3,5,9], 7: [4,8], 8: [7,5,9,0], 9: [6,8]}
I will be traversing through all digits and will append one of the possible neighbors to it until required length is achieved.
EDIT: Updated neighbors, no diagonals allowed
EDIT 2: Digits can be reused
Try this.
This will produce all possible sequences. You didn’t mention if you wanted ones that have cycles in them, for example
(0, 8, 9, 8)so I left them in. If you don’t want them, then just useNote that I made the entry for
0a list with one element instead of just an integer. This is for consistency. It’s very nice be able to index into the dictionary and know that you’re going to get a list back.Also note that this isn’t recursive. Recursive functions are great in languages that properly support them. In Python, you should almost always manage a stack like I demonstrate here. It’s just as easy as a recursive solution and sidesteps function call overhead (python doesn’t support tail recursion) and maximum recursion depth concerns.