Привет, я писал программу обнаружения плагиата.
Объяснение
В основном я пишу функцию , которая принимает ввод как 2 строки . Функция должна найти, если в обеих строках есть 5 или более последовательных слов. Строка будет состоять только из строчных букв и пробелов: без знаков препинания, из заглавных букв.
Требование
Если есть такие слова, вернуть самую длинную такую строку слов (в терминах количества слов, а не длины строки).Если нет, следует вернуть логическое значение False.Я написал некоторый код, и я не знаю, где он идет не так.
Мой прогресс
def check_plagiarism(str1,str2):
list1=str1.split()
list2=str2.split()
new1=[]
new2=[]
for i in list1:
if (i in list2):
new1.append(i)
for j in list2:
if (j in list1):
new2.append(j)
ans=[]
for i in range(0,len(new1)-1):
for j in range(0,len(new2)-1):
while new1[i]==new2[j]:
val=new1[i]
ans.append(val)
i+=1
j+=1
if i==len(new1) or j==len(new2):
return False
if len(ans)>=5:
value=" ".join(ans)
return value
else:
ans=[]
value=" ".join(ans)
return value
Мне удалось написать эту функцию.Я знаю, что он довольно длинный и неэффективный, но работает abit.
Ввод
Я предоставил следующий вход для функции.
a="i took a walk around the world to ease my troubled mind i left my body lying somewhere in the sands of time i watched the world float to the dark side of the moon i feel there is nothing i can do yeah i watched the world float to the dark side of the moon after all i knew it had to be something to do with you i really dont mind what happens now and then as long as youll be my friend at the end if i go crazy then will you still call me superman if im alive and well will you be there holding my hand ill keep you by my side with my superhuman might kryptonite"
b="i dont care if i go crazy then one two three four five switch crazy go i if care dont i five four three two one and switch"
c="when i was young i took a walk around the woods i found that i was both taller and smaller than the trees returning to my home i set out for the desert i journeyed for long days and nights my spirit left my body and i left my body lying somewhere in the sands of time unburdened by physical form i watched the world float away from me and into the vast moonlight"
print(check_plagiarism(a,b))
print(check_plagiarism(a,c))
print(check_plagiarism(b,c))
Полученный результат
if i go crazy then
took a walk around the
False
Ожидаемый результат
if i go crazy then
i left my body lying somewhere in the sands of time
False
Любая помощь будет оценена