Optimized by factors 3 and 10 respectively (on my benchmark case, but it can vary of course).
Explanation:
the function voyelle re-creates a list every time it's called whereas we could just create it once. Additionally, sets are a better structure to test whether an element belongs to a collection. In that case it's a constant operation anyway, but for an arbitrary size, the linear cost of going through a list would make a huge change compared to the constant cost of searching in a hash set
pop at arbitrary positions of an array in Python is very inefficient because it requires shifting the whole array. Simply using a counter instead of popping is much faster. The other changes in this one are just refactoring to avoid a few useless checks
Interestingly, I tried changing palindrome to check only the first half vs second half (the current version being redundant) but it wasn't faster.
Optimized by factors 3 and 10 respectively (on my benchmark case, but it can vary of course).
Explanation:
voyelle
re-creates a list every time it's called whereas we could just create it once. Additionally, sets are a better structure to test whether an element belongs to a collection. In that case it's a constant operation anyway, but for an arbitrary size, the linear cost of going through a list would make a huge change compared to the constant cost of searching in a hash setpop
at arbitrary positions of an array in Python is very inefficient because it requires shifting the whole array. Simply using a counter instead of popping is much faster. The other changes in this one are just refactoring to avoid a few useless checksInterestingly, I tried changing
palindrome
to check only the first half vs second half (the current version being redundant) but it wasn't faster.