logoalt Hacker News

stemchartoday at 9:50 AM4 repliesview on HN

I probably would've done the same. "I don't remember what the function is called" would've been fine-ish, but reaching for a regex is just insane.


Replies

StilesCrisistoday at 1:56 PM

Why? Unless it's an extraordinarily hot code path, it doesn't matter. A regex once compiled will be quite efficient.

show 3 replies
roenxitoday at 11:11 AM

A regexp basically comes with a compiler. Who knows what sort of optimisations they've built in under the hood. It wouldn't be surprising if there was a special fast-path for efficiently searching for a substring; that'd be effective in practice.

But more importantly it is hugely context sensitive on how often the function is going to be called and what IO needs to happen around it to decide if speed matters at all.

Using a regex as a first attempt is entirely reasonable. Especially in an interview about Python. If we care about efficiently doing substring matching Python isn't the language of choice. If a programmer just wants to remember how regex work and get on with their day they'll do fine at handling string problems.

show 1 reply
cucumber3732842today at 11:02 AM

It kind of depends on the substring and problem context.

Arbitrary substring in arbitrary text vs extracting embedded plant code from product serial numbers.

As long as you've got a good explanation for what you chose and why you chose it and the pro/con it's probably fine.

teiferertoday at 11:41 AM

Sarcastic or for real? Because I find that an obvious choice, a little depending on context though.