[Tutor] regexp

Albert-Jan Roskam fomcl at yahoo.com
Sat Nov 5 10:18:43 CET 2011


Hi,

I would know if or how it could be done with regexes, but isn't the following simple code a solution? It requires that the text be split up into separate words. Or am I overlooking something?

>>word = 'ym'
>>> [letter for n, letter in enumerate(word) if letter > word[n-1]] == list(word[1:])

False

>>word = 'almost'
>>> [letter for n, letter in enumerate(word) if letter > word[n-1]] == list(word[1:])
 True


Cheers!!
Albert-Jan


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


>________________________________
>From: Dave Angel <d at davea.name>
>To: Steven D'Aprano <steve at pearwood.info>
>Cc: tutor at python.org
>Sent: Saturday, November 5, 2011 2:49 AM
>Subject: Re: [Tutor] regexp
>
>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
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>To unsubscribe or change subscription options:
>http://mail.python.org/mailman/listinfo/tutor
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111105/635e5d57/attachment.html>


More information about the Tutor mailing list