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 6920965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:12:57+00:00 2026-05-27T10:12:57+00:00

I implemented an n-dimensional longest common subsequence in python, but it’s buggy in 3d.

  • 0

I implemented an n-dimensional longest common subsequence in python, but it’s buggy in 3d.

# -*- coding: utf-8 -*-
from collections import defaultdict
from operator import itemgetter

def count(limits):
    idcs = [0] * len(limits)
    while True:
        yield tuple(idcs)
        for n in range(len(limits)-1, -1, -1):
            idcs[n] += 1
            if idcs[n] != limits[n]:
                break
            elif n == 0:
                raise StopIteration
            else:
                idcs[n] = 0

def make_grid(words):
    size = len(words)
    default_backpointer = tuple([-1]*size)
    grid = defaultdict(lambda: (0, default_backpointer))
    for indices in count([len(word) for word in words]):
        print(indices)
        chars = {L[ind] for L, ind in zip(words, indices)}
        if len(chars) == 1:
            backpointer = tuple([coord - 1 for coord in indices])
            grid[indices] = (grid[backpointer][0] + 1, backpointer, chars.pop())
        else:
            iter = count([2]*size)
            iter.next() # To avoid self-reference
            values = []
            for previous in iter:
                backpointer = tuple([indices[index] - previous[index] for index in xrange(size)])
                values.append((grid[backpointer][0], backpointer, None))
            grid[indices] = max(values, key=itemgetter(0))
    print(grid.keys())
    return grid

def lcs(*words):
    grid = make_grid(words)
    best = []
    position = tuple([len(word) - 1 for word in words])

    while -1 not in position:
        pointer = grid[position]
        best.append(pointer[2])
        position = pointer[1]

    best = [item for item in best if item is not None ]
    best.reverse()
    return best

#print(lcs("HUMAN", "CHIMPANZEE", "WOMAN"))
print(lcs("MAN", "MAFN", "FMAN"))

Output:
[‘N’]

  • 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-27T10:12:57+00:00Added an answer on May 27, 2026 at 10:12 am

    Found the bug. itemgetter(0) for the max function, therefore wrong backpointers.

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

Sidebar

Related Questions

Implemented the Sink Class - to receive event notifications from COM Server Event Interface
Having implemented CLogClass to make decent logging I also defined macro, but it works
Python does not provide built-in support for multi-dimensional arrays. I need to develop an
I'm using sortable to implement a one-dimensional list of widgets. It's working alright, but
Currently I have my code putting user input into a one-dimensional ArrayList, but I
I want to create class representing n-dimensional array, but where is a commutative access
This is more an algorithm question than programming, but it's implemented in programming so
Implemented a generic repository with several Methods. One of those is this: public IEnumerable<T>
I implemented a Lucene search solution awhile back, and it got me interested in
I implemented a small OOP library in Lua, and two things are not quite

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.