Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3401866
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:04:05+00:00 2026-05-18T05:04:05+00:00

I am trying to generate all possible keypad sequences (7 digit length only right

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:04:06+00:00Added an answer on May 18, 2026 at 5:04 am

    Try this.

     neighbors = {0: [8], 
                 1: [2,4], 
                 2: [1,4,3], 
                 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]}
    
    
    def get_sequences(n):
        if not n:
            return
        stack = [(i,) for i in  range(10)]
        while stack:
            cur = stack.pop()
            if len(cur) == n:
                yield cur
            else:
                stack.extend(cur + (d, ) for d in neighbors[cur[-1]]) 
    
    print list(get_sequences(3))
    

    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 use

     stack.extend(cur + (d, ) 
                  for d in neighbors[cur[-1]]
                  if d not in cur)
    

    Note that I made the entry for 0 a 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to generate a list of all possible number combinations within a
Algorithm to generate all possible letter combinations of given string down to 2 letters
While trying to generate classes from a xsd, i got this error: java.lang.IllegalArgumentException: Illegal
I am trying to generate a report by querying 2 databases (Sybase) in classic
I'm trying to generate a sitemap.xml on the fly for a particular asp.net website.
I'm trying to generate customized xml files from a template xml file in python.
I am trying to generate a Sandcastle help file for a website. In the
I am trying to generate equivalent MD5 hashes in both JavaScript and .Net. Not
I'm trying to generate a unique ID in php in order to store user-uploaded
I'm trying to generate some code at runtime where I put in some boiler-plate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.