[Tutor] regexp
Dinara Vakhitova
di.marvellous at gmail.com
Sat Nov 5 00:06:44 CET 2011
Thank you for your answer, Steven.
Of course it would have been easier to write this function,
but unfortunately my task is to do it with a regular expression :(
D.
2011/11/5 Steven D'Aprano <steve at pearwood.info>
> 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)
>
>
>
> --
> Steven
> ______________________________**_________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>
--
*Yours faithfully,
Dinara Vakhitova*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111105/a8cd1565/attachment.html>
More information about the Tutor
mailing list