Greedy String Tiling in Python

2024/7/7 10:37:09

I am trying to learn greedy string tiling in algorithm

I have two lists as follows:

a=['a','b','c','d','e','f']
b=['d','e','a','b','c','f']

i would like to retrieve c=['a','b','c','d','e']

Another example would be

a = ['1','2','3','4','5','6','7','8','9','1','3']
b = ['3','4','5','2','1','7','8','9','1']

c should be ['3','4','5','7','8','9','1']

Right now I am using the following code which works for the latter example but not the former. Can someone help?

def gct(a, b):if len(a) == 0 or len(b) == 0:return []if a[0] == b[0]:return [a[0]] + gct(a[1:], b[1:])return max(gct(a, b[1:]), gct(a[1:], b), key=len)

I am calling function with

gct( ['a','b','c','d','e','f'], ['d','e','a','b','c','f'] )

which gives ['a', 'b', 'c', 'f'], when it should be ['a','b','c','d','e'] or ['d','e','a','b','c'].

P.S The order doesn't matter in the printing of the result. The order is only important while doing the comparisons. The minimum pattern length should be 2

NOTE intersect will not solve my problem

Answer

The following codes can solve the problem. A minlength is added to make sure the substr >= minlength.

  • maxsub is used to find the longest substr and return its index in string a.
  • markit.a is used to mark the char position found before in string a.
  • while loop is used to find all the substr (len>=minlength)

^

#! /usr/bin/env python
a=['a','b','c','d','e','f']
b=['d','e','a','b','c','f']a = ['1','2','3','4','5','6','7','8','9','1','3']
b = ['3','4','5','2','1','7','8','9','1']def gct(a,b,minlength=2):if len(a) == 0 or len(b) == 0:return []# if py>3.0, nonlocal is betterclass markit:a=[0]minlen=2markit.a=[0]*len(a)markit.minlen=minlength#output char indexout=[]# To find the max length substr (index)# apos is the position of a[0] in origin stringdef maxsub(a,b,apos=0,lennow=0):if (len(a) == 0 or len(b) == 0):return []if (a[0]==b[0] and markit.a[apos]!=1 ):return [apos]+maxsub(a[1:],b[1:],apos+1,lennow=lennow+1)elif (a[0]!=b[0] and lennow>0):return []return max(maxsub(a, b[1:],apos), maxsub(a[1:], b,apos+1), key=len)while True:findmax=maxsub(a,b,0,0)if (len(findmax)<markit.minlen):breakelse:for i in findmax:markit.a[i]=1out+=findmaxreturn [ a[i] for i in out]print gct(a,b)
>> ['a', 'b', 'c', 'd', 'e']
>> ['7', '8', '9', '1', '3', '4', '5']
print gct(a,b,3)
>> ['a', 'b', 'c']
>> ['7', '8', '9', '1', '3', '4', '5']
https://en.xdnf.cn/q/119568.html

Related Q&A

Python - efficient way to create 20 variables?

I need to create 20 variables in Python. That variables are all needed, they should initially be empty strings and the empty strings will later be replaced with other strings. I cann not create the var…

Whatsapp asking for updating chrome version

I am trying to open whatsapp with selenium and python, it was working fine until today. In headless or non, whatsapp is now asking to update chrome, when I try to do so, Chrome throws this error: An er…

how to find the longest N words from a list, using python?

I am now studying Python, and I am trying to solve the following exercise:Assuming there is a list of words in a text file, My goal is to print the longest N words in this list.Where there are several …

([False, True] and [True, True]) evaluates to [True, True]

I have observed the following behavior in python 3: >>> ([False, True] and [True, True]) [True, True]>>> ([False, True] or [True, True]) [False, True]I was expecting exactly the oppos…

Uploading an image to Flask server

I am struggling bit with Flask and uploading a file, here is my Flask code so far:@app.route(/api/user/update/, methods=[PUT]) @auth.login_required def update_user():# check if the post request has the…

Enemy Projectiles Arent Appending On Screen

I have here my script that targets the player what ever position he is at but the projectiles arent showing on my screen VIDEO. He isnt attacking at all, I dont know why. I am in my main loop I draw t…

Python+Selenium. Cant locate element

Ive implemented the script using Python and selenium to click on the ads. But now this script is not working.Unable to find element on the page.Please help me to correct the script. Thank you!from sele…

AttributeError: NoneType object has no attribute channels [duplicate]

This question already has answers here:Why do I get AttributeError: NoneType object has no attribute something?(11 answers)Closed 5 years ago.Hi Im having an issue with a module for my Discord bot. Im…

How to get a specific value from a html header

Im using selenium to get request headers from a web page, the problem is that it prints out all request headers sent and i want to get only one value from one of them. I dont know how to do it and i ha…

How to use windows as raspberry pi and connect the windows with another raspberry pi [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 4…