[Tutor] regexp

Dave Angel d at davea.name
Sat Nov 5 02:49:14 CET 2011


On 11/04/2011 07:00 PM, Steven D'Aprano wrote:
> Dinara Vakhitova wrote:
>> Hello,
>>
>> I need to find the words in a corpus, which letters are in the 
>> alphabetical
>> order ("almost", "my" etc.)
>
> Quoting Jamie Zawinski:
>
>     Some people, when confronted with a problem, think "I know, I'll
>     use regular expressions." Now they have two problems.
>
> Now you have two problems: find words in the corpus which are in 
> alphabetical order, and get the damn regular expression to work 
> correctly.
>
> Don't use a regex for this. It is much simpler to write a Python 
> function to solve it:
>
> def letters_in_order(astring):
>     """Return True if letters in astring are in alphabetical order.
>
> >>> letters_in_order("almost")
>     True
> >>> letters_in_order("zoology")
>     False
>
>     """
>     if len(astring) <= 1:
>         return True
>     for i in range(1, len(astring)):
>         if astring[i] < astring[i-1]:
>            # Pair of characters are out of order.
>             return False
>     # If none of the pairs are out of order, they whole string
>     # must be in order.
>     return True
>
> words = filter(letters_in_order, corpus)
> for word in words:
>     print(word)
>
>
>
Seems to me it'd much simpler to do something like:
     return "".join(sorted(astring)) == astring

with suitable adjustment if both lower and uppercase are desirable.

-- 

DaveA



More information about the Tutor mailing list