From a5d93226408e179cdd8db4a6f5796985c23d2cf6 Mon Sep 17 00:00:00 2001 From: Kayode Date: Fri, 11 Nov 2022 15:45:21 +0100 Subject: [PATCH] Kayode-week9. Longest Common Subsequence --- week9/longest_common_subsequence.PY | 46 +++++++++++++++++++++++++++++ week9/longest_common_subsequence.md | 14 +++++++++ 2 files changed, 60 insertions(+) create mode 100644 week9/longest_common_subsequence.PY create mode 100644 week9/longest_common_subsequence.md diff --git a/week9/longest_common_subsequence.PY b/week9/longest_common_subsequence.PY new file mode 100644 index 0000000..2ea7a6b --- /dev/null +++ b/week9/longest_common_subsequence.PY @@ -0,0 +1,46 @@ +from itertools import combinations + +def LCS(strArr): + first= strArr[0] + second= strArr[1] + + firstsubs= [] + secondsubs= [] + for i in range(len(first)): + comb1= list(combinations(first, i)) + comb2=[] + for j in comb1: + comb2.append(''.join(j)) + firstsubs.extend(comb2) + + + for i in range(len(second)): + comb3= list(combinations(second, i)) + comb4=[] + for j in comb3: + comb4.append(''.join(j)) + secondsubs.extend(comb4) + + firstsubs.remove('') + secondsubs.remove('') + if firstsubs == []: + firstsubs.append(first) + if secondsubs == []: + secondsubs.append(second) + + sublength= 0 + + for i in firstsubs: + for j in secondsubs: + if i == j: + if len(i) > sublength: + sublength= len(i) + + + print(sublength) + + + +# LCS(['bcacb', 'aacabb']) +LCS(['a', 'aa']) +# LCS(['c', 'b']) \ No newline at end of file diff --git a/week9/longest_common_subsequence.md b/week9/longest_common_subsequence.md new file mode 100644 index 0000000..eb5cb7a --- /dev/null +++ b/week9/longest_common_subsequence.md @@ -0,0 +1,14 @@ +--- +Author: Kayode +--- + +Longest Common Subsequence (LCS) +Have the function LCS(strArr) take the strArr parameter being passed which will be an array of two strings containing only the characters {a,b,c} and have your program return the length of the longest common subsequence common to both strings. A common subsequence for two strings does not require each character to occupy consecutive positions within the original strings. For example: if strArr is ["abcabb","bacb"] then your program should return 3 because one longest common subsequence for these two strings is "bab" and there are also other 3-length subsequences such as "acb" and "bcb" but 3 is the longest common subsequence for these two strings. + + +Examples +Input: ["abc","cb"] +Output: 1 + +Input: ["bcacb","aacabb" +Output: 3 \ No newline at end of file