LINUX.ORG.RU

История изменений

Исправление tz4678, (текущая версия) :

Без встроенных либ что-то такое должно работать:

longest = 'шлакоблок'
shortest = 'окунь'
m = max(longest.find(shortest[:i]) for i in range(1, len(shortest) + 1))
longest[:m] + shortest

Исправление tz4678, :

Без встроенных либ что-то такое должно работать:

In [8]: longest = 'шлакоблок'

In [9]: shortest = 'окунь'

In [10]: [longest.find(shortest[:i]) for i in range(1, len(shortest) + 1)]
Out[10]: [4, 7, -1, -1, -1]

In [11]: [(longest.find(shortest[:i]), i) for i in range(1, len(shortest) + 1)]
Out[11]: [(4, 1), (7, 2), (-1, 3), (-1, 4), (-1, 5)]

In [12]: max((longest.find(shortest[:i]), i) for i in range(1, len(shortest) + 1))
Out[12]: (7, 2)

In [13]: a, b = max((longest.find(shortest[:i]), i) for i in range(1, len(shortest) + 1))

In [14]: longest[:a]
Out[14]: 'шлакобл'

In [15]: longest + shortest[b:]
Out[15]: 'шлакоблокунь'

Исходная версия tz4678, :

Без встроенных либ что-то такое должно работать:

In [8]: longest = 'шлакоблок'

In [9]: shortest = 'окунь'

In [10]: [longest.find(shortest[:i]) for i in range(1, len(shortest) + 1)]
Out[10]: [4, 7, -1, -1, -1]

In [11]: [(longest.find(shortest[:i]), i) for i in range(1, len(shortest) + 1)]
Out[11]: [(4, 1), (7, 2), (-1, 3), (-1, 4), (-1, 5)]

In [12]: max((longest.find(shortest[:i]), i) for i in range(1, len(shortest) + 1))
Out[12]: (7, 2)

In [13]: a, b = max((longest.find(shortest[:i]), i) for i in range(1, len(shortest) + 1))

In [14]: longest[:a]
Out[14]: 'шлакобл'

In [15]: longest[:a] + shortest[b:]
Out[15]: 'шлакоблунь'