Skip to content
Advertisement

How to optimise function logic to avoid duplication – python

I have some code below, for a given name that matches pattern with 2 lower case letters (such as ‘aa’), the valid name can be V-aa or X-aa

JavaScript

My question is: can I do something like:

JavaScript

Advertisement

Answer

From your description, I assume that verify_helper() is computationally expensive regardless of names being valid.

Now you want to optimize the code, by having verify_helper() only execute for valid names.

If that’s the case, you could have the condition inside verify_helper(), like this.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement