[Tutor] Select a string

Cameron Simpson cs at cskk.id.au
Wed Sep 6 17:16:14 EDT 2017


On 06Sep2017 09:12, Pat Martin <wpmartin at gmail.com> wrote:
>I got it working for all the tests, but the code is ugly. I think regex or
>using string methods would have been easier and neater but like I said we

Always go for string methods first. Regexps are cryptic and easy to get wrong, 
and relatively hard to debug when they are wrong. And they're more expensive to 
create and to run.

The flip side is that there are things which can be said easily and concisely 
with regexps. But you should generally consider string methods first.

>hadn't covered it in the class yet so didn't think I should use it. I am
>embarrassed by how bad the code looks to me.

That comes with practice: getting intuition about what reads well, and what 
tends to be error prone or fragile.

For the exercise, have a relook at the code you have. What would make it easier 
to read? Easier to debug? Better variable names? Even trivial changes like 
changing "firstb" to "first_b" and "theo" to "the_o" can help. Would things be 
clearer with a leading comment explaining the purposes of the state variables:

  # Scan a string for instances of "bob", including overlaps like "bobob".
  # State variables:
  #  first_b: set on encountering the first "b" of a potential "bob"
  #  the_o: set on encountering the :o"  of a potential "bob"; only set if the
  #    preceeding character was the "b" (first_b).

Partiularly for little state machines like yours I often find such descriptions 
useful.  They help get the logic clear in my own mind _before_ writing the 
loop, and help you and others understand the code later when you come back to 
debug.

It can also help to comment the individual if-statements. Eg:

  # recognise the "o" of "bob" if the preceeding character was "b"

Cheers,
Cameron Simpson <cs at cskk.id.au> (formerly cs at zip.com.au)


More information about the Tutor mailing list