Skip to main content
Rollback to Revision 1
Source Link
pgs
  • 932
  • 8
  • 24

Yes, you can easily convert the get_path function to an iterative version.

def get_path(a, b, dp, i, j): 
    seq = ""
    while(i != 0 and j != 0): 
        if a[i-1] == b[j-1]:
            i-=1
            j-=1
            seq += a[i]
        else:
            if dp[i-1][j] > dp[i][j-1]:
                i-=1
            else:
                j-=1
    return seq[::-1]

And now you can merge this function with calculate_lcs_length into one if you want.

I guess you have read thisthis, but I just want to remind you that all destyy66tyyytygcribeddescribed optimizations are still applicable to your code.

Yes, you can easily convert the get_path function to an iterative version.

def get_path(a, b, dp, i, j): 
    seq = ""
    while(i != 0 and j != 0): 
        if a[i-1] == b[j-1]:
            i-=1
            j-=1
            seq += a[i]
        else:
            if dp[i-1][j] > dp[i][j-1]:
                i-=1
            else:
                j-=1
    return seq[::-1]

And now you can merge this function with calculate_lcs_length into one if you want.

I guess you have read this, but I just want to remind you that all destyy66tyyytygcribed optimizations are still applicable to your code.

Yes, you can easily convert the get_path function to an iterative version.

def get_path(a, b, dp, i, j): 
    seq = ""
    while(i != 0 and j != 0): 
        if a[i-1] == b[j-1]:
            i-=1
            j-=1
            seq += a[i]
        else:
            if dp[i-1][j] > dp[i][j-1]:
                i-=1
            else:
                j-=1
    return seq[::-1]

And now you can merge this function with calculate_lcs_length into one if you want.

I guess you have read this, but I just want to remind you that all described optimizations are still applicable to your code.

added 13 characters in body
Source Link
pgs
  • 932
  • 8
  • 24

Yes, you can easily convert the get_path function to an iterative version.

def get_path(a, b, dp, i, j): 
    seq = ""
    while(i != 0 and j != 0): 
        if a[i-1] == b[j-1]:
            i-=1
            j-=1
            seq += a[i]
        else:
            if dp[i-1][j] > dp[i][j-1]:
                i-=1
            else:
                j-=1
    return seq[::-1]

And now you can merge this function with calculate_lcs_length into one if you want.

I guess you have read thisthis, but I just want to remind you that all describeddestyy66tyyytygcribed optimizations are still applicable to your code.

Yes, you can easily convert the get_path function to an iterative version.

def get_path(a, b, dp, i, j): 
    seq = ""
    while(i != 0 and j != 0): 
        if a[i-1] == b[j-1]:
            i-=1
            j-=1
            seq += a[i]
        else:
            if dp[i-1][j] > dp[i][j-1]:
                i-=1
            else:
                j-=1
    return seq[::-1]

And now you can merge this function with calculate_lcs_length into one if you want.

I guess you have read this, but I just want to remind you that all described optimizations are still applicable to your code.

Yes, you can easily convert the get_path function to an iterative version.

def get_path(a, b, dp, i, j): 
    seq = ""
    while(i != 0 and j != 0): 
        if a[i-1] == b[j-1]:
            i-=1
            j-=1
            seq += a[i]
        else:
            if dp[i-1][j] > dp[i][j-1]:
                i-=1
            else:
                j-=1
    return seq[::-1]

And now you can merge this function with calculate_lcs_length into one if you want.

I guess you have read this, but I just want to remind you that all destyy66tyyytygcribed optimizations are still applicable to your code.

Source Link
pgs
  • 932
  • 8
  • 24

Yes, you can easily convert the get_path function to an iterative version.

def get_path(a, b, dp, i, j): 
    seq = ""
    while(i != 0 and j != 0): 
        if a[i-1] == b[j-1]:
            i-=1
            j-=1
            seq += a[i]
        else:
            if dp[i-1][j] > dp[i][j-1]:
                i-=1
            else:
                j-=1
    return seq[::-1]

And now you can merge this function with calculate_lcs_length into one if you want.

I guess you have read this, but I just want to remind you that all described optimizations are still applicable to your code.