Hacker News new | ask | show | jobs
by WJW 937 days ago
The problem statement was super clear though. "Find the first occurrence of any one of these strings in a longer string" doesn't require any fancy regex tricks, just a for loop and knowledge about `isPrefixOf` or `startsWith` or whatever the equivalent function is called in your language of choice.

"Find the last occurrence of any one of these strings in a longer string" is just the first problem again but with all the strings reversed.

2 comments

Disagree that it's clear. If the text is "oneight", there is a legitimate philosophical question about whether the string contains two numbers. I feel like most people would say no, because the only way to answer yes is by re-using letters between different words, and that's not how language works.
> knowledge about `isPrefixOf` or `startsWith` or whatever the equivalent function is called in your language of choice.

There's no guarantee the digits are the first or last, so it's more `find` and `rfind`, unless you try every subslice of the line by hand.

Although thinking about it assuming the lines are not too long I guess that also works.

I looked at the slices from each position to the end. Now for every name (“one”, “1”, “two”, …) check slice.startswith(name).

After I pulled out first and last from results array.

Two nested for loops, program all included was under twenty lines.