Thread: Troubleshooting Longest Increasing Subsequence

  1. #1
    Banned
    Join Date
    Oct 2022
    Posts
    5

    Troubleshooting Longest Increasing Subsequence

    I'm having a bit of difficulty trying to implement the longest increasing subsequence algorithm. I found a <<crapware site>> online that looks helpful, but I'm having trouble translating the pseudocode into code.


    I'm using Python, and I've written the following function:


    Code:
    def longest_increasing_subsequence(arr):
        n = len(arr)
        lis = [1]*n
        
        for i in range(1, n):
            for j in range(0, i):
                if arr[i] > arr[j] and lis[i] < lis[j] + 1 :
                    lis[i] = lis[j]+1
     
        maximum = 0
     
        for i in range(n):
            maximum = max(maximum, lis[i])
     
        return maximum


    When I try to run the code, I get the following error:


    Code:
    NameError: name 'arr' is not defined
    I'm not sure what I'm doing wrong. Any help would be appreciated!
    Thank you.
    Last edited by Salem; 04-18-2023 at 01:11 PM. Reason: Yet another spam post disguised as a question.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like the error lies in code that you did not show, e.g., you may have made a mistake when calling the function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generate all subsequence combinations
    By Freaky256 in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2015, 11:04 PM
  2. Replies: 1
    Last Post: 03-16-2012, 02:07 AM
  3. Max SubSequence using recursion
    By Amoxaphobic in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2011, 02:46 PM
  4. Minimum Positive Subsequence Sum
    By CrazyNorman in forum C++ Programming
    Replies: 2
    Last Post: 09-11-2008, 04:25 AM
  5. Longest Common Subsequence
    By stimpyzu in forum Tech Board
    Replies: 4
    Last Post: 04-04-2005, 03:18 PM

Tags for this Thread