two-way for `str.rfind`
Ninguém assumiu esta issue ainda.
- Linguagem predominante
- Python
- Estrelas
- 77.2k
- Forks
- 36k
- Métricas de merge de PRs
- Métricas de PR pendentes
Descrição
Feature or enhancement
Proposal:
After closing with: https://github.com/python/cpython/pull/120025#issuecomment-5281829097
I appreciate that str.rfind is much less frequently used than str.find. But as can be seen it the PR above, it is fairly easy to make two_way bi-directional to handle both. I concluded that for standard track Horspool algorithm the cost of bi-directional logic is best avoided - those methods are fairly short and simple. However, two-way is a plausible candidate I think. Having separate functions for both direction would mean very large code duplications and I think paying a small perf cost to keep single logic for both might be a fairly good deal.
I think it would be good to do it - single string search is one of the most important low level algorithms - having this space clean and complete is I think a good thing -- it would give peace of mind for developers and predictable behaviour for users, and library developers who make heavy use of string search, allowing to derive new algorithms that work in both directions without performance surprises when running in reverse.
There is a workaround of course:
idx = str[::-1].find(sub[::-1])
if idx != -1:
idx = len(str) - len(sub) - idx
However, for small problem sizes this is a non-trivial overhead + double memory.
So, what are the thoughts of others?
Is adapting two-way search to serve str.rfind (eliminating its O(n^2) worst case) desirable/worthwhile?
cc @methane @rhettinger @serhiy-storchaka @vstinner
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Direção de pesquisa
Comece pelo ponto de entrada str.rfind e pela lógica de busca two_way existente discutida no pull request vinculado. Compare os requisitos das buscas para frente e para trás e, em seguida, avalie se o tratamento bidirecional pode evitar o pior caso O(n^2) de str.rfind sem um custo de desempenho inaceitável ou funções duplicadas. Considera-se concluído quando o design estiver acordado e seu comportamento e suas compensações tiverem sido validados.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- python
- Domínio
- performance
- Tipo de issue
- Funcionalidade
- Dificuldade
- 5/5
- Tempo estimado
- Mais de uma semana
- Status de atividade
- Pouca atividade
- Clareza
- Precisa de esclarecimento
- Facilidade para iniciantes
- 35/100