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

  • SEARCH
  • Home
  • 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 3483788
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:42:52+00:00 2026-05-18T10:42:52+00:00

i have a matrix 1 9 2 3 5 0 0 6 8 4

  • 0

i have a matrix

1 9 2 3
5 0 0 6
8 4 4 8
2 3 7 8

I need to find all possible combinations of numbers of length 5.

constraints:

  1. Starting form a any position in the matrix you can only move to your next immediate neighbor i.e if u start form say (0,0) your neighbor must be (0,1),(1,1),(1,0) and if you pick a position then form that position you can only move to its immediate neighbor and so on.

  2. The length of the number must be 5 digit’s i.e for example if i start from (0,0) with value 1 i can produce a sequence 15151 or 19232 or 10063 so on you can move in any sequence with the constraint 1 applied.

  3. The solution must produce the output in 7sec and python is preferred since its my favorite. 😉

OK i missed some things the program must use all 16 numbers i.e it must use all the 16 numbers as initial and produce the 5 digit sequence.

  • 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-18T10:42:52+00:00Added an answer on May 18, 2026 at 10:42 am

    First, you have to think about how to start at one position in the matrix and move to an adjacent one.

    The brute force method is simply to list all available cells and all adjacent cells for each:

    nextpos = {
        (0,0): [(1,0), (1,1), (0,1)],
        (0,1): [(0,0), (1,0), (1,1), (1,2), (0,2)],
        (0,2): [(0,1), (1,1), (1,2), (1,3), (0,3)],
        (0,3): [(0,2), (1,2), (1,3)],
        # etc
    }
    allpos = nextpos.keys()
    

    For a problem this small, this is pretty simple; however, there is always a chance of typos. Another solution is to write a generator function:

    def nextpos(p,w=4,h=4):
        """
        @param p     Current position tuple (x,y)
        @param w     Width of matrix
        @param h     Height of matrix
    
        Generate all matrix cells adjacent to the current one
        """
        rel = ((-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1))
    
        x,y = p
        for dx,dy in rel:
            nx,ny = x+dx, y+dy
            if 0<=nx<w and 0<=ny<h:
                yield (nx,ny)
    

    Once we know which cells are next to each other, we can proceed to seek valid paths:

    def matrix_path(pathLen, startFrom=None):
        """
        @param pathLen    Length of path to return
        @param startFrom  Initial location
    
        Generate all adjacency paths through the matrix
        """
    
        # bad path length - quit gracefully
        if pathLen<1:
            yield []
            return
    
        # no starting point specified - start at any point
        if startFrom is None:
            for pos in allpos:
                for res in matrix_path(pathLen, pos):
                    yield res
            return
    
        # end of path
        if pathLen==1:
            yield [startFrom]
            return
    
        # from current point, recurse to find rest of path
        for pos in nextpos[startFrom]:
            for morePath in matrix_path(pathLen-1, pos):
                yield [startFrom]+morePath
    

    We can find out how long this takes by profiling:

    import cProfile
    
    def test():
        sols = [i for i in matrix_path(5)]
        print len(sols), "paths found"
    
    cProfile.run("test()")
    

    which returns

    16860 paths found
    121497 function calls (16865 primitive calls) in 0.678 CPU seconds

    This returns a list of lists of cell-positions; we want to turn this into the actual values from the matrix,

    def path_vals(mx, path):
        """
        @param mx    Matrix data
        @param path  List of cell positions
    
        Return list of values from list of cell positions
        """
    
        return tuple([mx[x][y] for x,y in path])
    

    Then

    mx = [
        [1,9,2,3],
        [5,0,0,6],
        [8,4,4,8],
        [2,3,7,8]
    ]
    
    def test():
        sols = [path_vals(mx, i) for i in matrix_path(5)]
    

    We also want to reduce the list to unique results:

    def test():
        usol = list(set([path_vals(mx, i) for i in matrix_path(5)]))
        print len(usol),"unique results"
    
    cProfile.run("test()")
    

    which gives us

    8651 unique results
    138357 function calls (33725 primitive calls) in 0.845 CPU seconds

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

Sidebar

Related Questions

I have a matrix and I need to find a pattern inside this matrix.
I have a matrix in MATLAB and I need to find the 99% value
everyone. I need to find matrix n*n (or 5*5 ) determinant. I have a
I have a nxm matrix and I need to find the maximum of sum
I have a 2D matrix that I need to add into a 3D matrix,
I have a feature matrix implemented with Silverlight's Grid where users need to select
I have a need for a sparse matrix in up to 4 dimensions in
I have a need to basically create a matrix of values in my Silverlight
I need to get rotation matrix from direction vector (vForward) I also have vRight
Have a matrix report now that has Position, Hours and Wages for a location

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.