From 8edf77b3bcd9d7126e9425c3e358a434d1977497 Mon Sep 17 00:00:00 2001 From: rishabh-28 Date: Fri, 2 Oct 2020 15:58:16 +0530 Subject: [PATCH 1/2] Desired result is coming out now. Changes the code to much optimised DP algorithm --- LowestCommenSubsequence.py | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/LowestCommenSubsequence.py b/LowestCommenSubsequence.py index e3fced4..17493d0 100644 --- a/LowestCommenSubsequence.py +++ b/LowestCommenSubsequence.py @@ -1,20 +1,12 @@ def lcs(X, Y): - # find the length of the strings m = len(X) - n = len(Y) - - # declaring the array for storing the dp values - L = [[None]*(n - 1) for i in range(m - 1)] - - for i in range(m + 1): - for j in range(n + 1): - if i == 0 or j == 0 : - L[i][j] = 0 - elif X[i-1] == Y[j-1]: - L[i][j] = L[i-1][j-1]+1 - else: - L[i][j] = max(L[i-1][j], L[i][j-1]) - return L[m][n] + n = len(Y) + if m == 0 or n == 0: + return 0; + elif X[m-1] == Y[n-1]: + return 1 + lcs(X, Y, m-1, n-1); + else: + return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n)); X = "AGGTAB" Y = "GXTXAYB" print("Length of LCS is ", lcs(X, Y)) From ef4cf8dd7aee6b453abc46e7d391a812d0f82cf1 Mon Sep 17 00:00:00 2001 From: rishabh-28 Date: Fri, 2 Oct 2020 16:11:58 +0530 Subject: [PATCH 2/2] Using in-built bin function to convert a decimal to binary --- Decimal To Binary.py | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/Decimal To Binary.py b/Decimal To Binary.py index 53ea7a8..8c3e993 100644 --- a/Decimal To Binary.py +++ b/Decimal To Binary.py @@ -1,18 +1,2 @@ -def decToBinary(n): - - # array to store - # binary number - binaryNum = [0] * n; - - # counter for binary array - i = 0; - while (n > 0): - - # storing remainder - binaryNum[i] = n % 2; - n = int(n / 2); - i += 1; - - # printing binary array - for j in range(i - 1, -1, -1): - print(binaryNum[j], end = ""); +def decimalToBinary(n): + print(bin(n).replace("0b","")) \ No newline at end of file