Created
January 24, 2019 16:48
-
-
Save dat-boris/47ff6317816d656282a24caf3f72fc95 to your computer and use it in GitHub Desktop.
Solving longest common subsequence from a set of words
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ A snippet for calculating longest subsequence of list of words | |
| """ | |
| import re | |
| # copy and paste seperation into here | |
| INPUT = filter(len, """ | |
| fuad-not-distressed | |
| arrows_counterclockwise | |
| """.split('\n')) | |
| # other test input | |
| #INPUT_0122 = filter(len, re.split(r'\s', ":flashlight: :hulk_smash: :flag-sh: :camera_with_flash:")) | |
| def lcs(X, Y, m=None, n=None): | |
| # print((X, Y, m, n)) | |
| if m is None: | |
| m = len(X) | |
| if n is None: | |
| n = len(Y) | |
| if m == 0 or n == 0: | |
| return "" | |
| elif X[m-1] == Y[n-1]: | |
| return lcs(X, Y, m-1, n-1) + X[m-1] | |
| else: | |
| x_lcs = lcs(X, Y, m, n-1) | |
| y_lcs = lcs(X, Y, m-1, n) | |
| return x_lcs if len(x_lcs) > len(y_lcs) else y_lcs | |
| if __name__ == "__main__": | |
| print(INPUT) | |
| # pre filter since above could be O(n^2) operation! | |
| agreed_chars = set.intersection(*map(set, INPUT)) | |
| print("Agreed set: {}".format(agreed_chars)) | |
| filtered_inputs = map( | |
| lambda s: "".join([c for c in s if c in agreed_chars]), | |
| INPUT | |
| ) | |
| print("Filtered input: {}".format(filtered_inputs)) | |
| last_lcs = "" | |
| for i in range(len(filtered_inputs)-1): | |
| last_lcs = lcs(filtered_inputs[i], filtered_inputs[i+1]) | |
| print("Last result is: {}".format(last_lcs)) | |
| print("Final LCS: {}".format(last_lcs)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment