Re: [Tutor] regexp
Dear Terry, Thank you for your advise, I'll try to implement it. D. 2011/11/6 Terry Carroll carr...@tjc.com On Sat, 5 Nov 2011, Dinara Vakhitova wrote: I need to find the words in a corpus, which letters are in the alphabetical order (almost, my etc.) I started with matching two consecutive letters in a word, which are in the alphabetical order, and tried to use this expression: ([a-z])[\1-z], but it won't work, it's matching any sequence of two letters. I can't figure out why... Evidently I can't refer to a group like this, can I? But how in this case can I achieve what I need? First, I agree with the others that this is a lousy task for regular expressions. It's not the tool I would use. But, I do think it's doable, provided the requirement is not to check with a single regular expression. For simplicity's sake, I'll construe the problem as determining whether a given string consists entirely of lower-case alphabetic characters, arranged in alphabetical order. What I would do is set a variable to the lowest permissible character, i.e., a, and another to the highest permissible character, i.e., z (actually, you could just use a constant, for the highest, but I like the symmetry. Then construct a regex to see if a character is within the lowest-permissible to highest-permissible range. Now, iterate through the string, processing one character at a time. On each iteration: - test if your character meets the regexp; if not, your answer is false; on pass one, this means it's not lower-case alphabetic; on subsequent passes, it means either that, or that it's not in sorted order. - If it passes, update your lowest permissible character with the character you just processed. - regenerate your regexp using the updated lowest permissible character. - iterate. I assumed lower case alphabetic for simplicity, but you could modify this basic approach with mixed case (e.g., first transforming to all-lower-case copy) or other complications. I don't think there's a problem with asking for help with homework on this list; but you should identify it as homework, so the responders know not to just give you a solution to your homework, but instead provide you with hints to help you solve it. __**_ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/**mailman/listinfo/tutorhttp://mail.python.org/mailman/listinfo/tutor -- *Yours faithfully, Dinara Vakhitova* ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] regexp
Steven, Walter, Dave, Peter and Albert, First of all, thank you very much for your suggestions, I appreciate a lot your help. I would only like to mention that I would have never asked something to be done for me just because it's my homework, I posted this question only because I couldn't find a solution myself and I was dying from curiosity how to do it. For me it makes no difference if it was homework or not, because it's practice first of all, I'm not studying for marks, that's why I was surprised to know that I should have specified it. So, yesterday I was so upset that I couldn't make it work that I went to bed and gave it up for a while :) But then I came up with the solution similar to that proposed by Walter and Peter, i.e. list all the letters in alphabetical order marked by *: [a*b*c*d*...x*y*z*] - with the only difference that I don't need to mark the beginning and the end of the line, because I first tokenized my text and I work with a single word (and it's supposed that the word is well-formed, without punctuation marks attached to it or empty strings or something) But naturally, this solution doesn't seem to be very elegant one (however, it might be exactly that solution that our teacher supposed us to find) I think, that I leave it like this till I find something more elegant if it's possible at all. 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 Albert, concerning your question - yes, the text is tokenized to separate words. Thank you for your suggestion! I'm trying to understand how it works now :) Kind Regards, Dinara 2011/11/5 Albert-Jan Roskam fo...@yahoo.com 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] regexp
Oh, sorry, of course it should be without brackets: 'a*b*c*...' [a*b*c*d*...x*y*z*] 2011/11/5 Dinara Vakhitova di.marvell...@gmail.com Steven, Walter, Dave, Peter and Albert, First of all, thank you very much for your suggestions, I appreciate a lot your help. I would only like to mention that I would have never asked something to be done for me just because it's my homework, I posted this question only because I couldn't find a solution myself and I was dying from curiosity how to do it. For me it makes no difference if it was homework or not, because it's practice first of all, I'm not studying for marks, that's why I was surprised to know that I should have specified it. So, yesterday I was so upset that I couldn't make it work that I went to bed and gave it up for a while :) But then I came up with the solution similar to that proposed by Walter and Peter, i.e. list all the letters in alphabetical order marked by *: [a*b*c*d*...x*y*z*] - with the only difference that I don't need to mark the beginning and the end of the line, because I first tokenized my text and I work with a single word (and it's supposed that the word is well-formed, without punctuation marks attached to it or empty strings or something) But naturally, this solution doesn't seem to be very elegant one (however, it might be exactly that solution that our teacher supposed us to find) I think, that I leave it like this till I find something more elegant if it's possible at all. 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 Albert, concerning your question - yes, the text is tokenized to separate words. Thank you for your suggestion! I'm trying to understand how it works now :) Kind Regards, Dinara 2011/11/5 Albert-Jan Roskam fo...@yahoo.com 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 -- *Yours faithfully, Dinara Vakhitova* ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] regexp
Hello, I need to find the words in a corpus, which letters are in the alphabetical order (almost, my etc.) I started with matching two consecutive letters in a word, which are in the alphabetical order, and tried to use this expression: ([a-z])[\1-z], but it won't work, it's matching any sequence of two letters. I can't figure out why... Evidently I can't refer to a group like this, can I? But how in this case can I achieve what I need? Thank you. -- *Yours faithfully, Dinara Vakhitova* ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] regexp
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 st...@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@python.org To unsubscribe or change subscription options: http://mail.python.org/**mailman/listinfo/tutorhttp://mail.python.org/mailman/listinfo/tutor -- *Yours faithfully, Dinara Vakhitova* ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] regexp
Sorry, I didn´t know that I couldn´t ask questions about the homework... I wanted to do it recursively, like this: def check_abc(string): string = string.lower() check_pair = re.compile(([a-z])[\1-z]) if check_pair.match(string): if check_abc(string[1:]): return True else: return False else: return False the only problem is that this regex doesn´t work and this case is not specified in the Python documentation... Excuse me for disturbing you. 2011/11/5 Steven D'Aprano st...@pearwood.info Dinara Vakhitova wrote: 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 :( Is this homework? You should have said so. I don't understand questions like this. Do carpenters ask their apprentices to cut a piece of wood with a hammer? Do apprentice chefs get told to dice carrots using only a spoon? Computer programming is the only skill I know of where teachers routinely insist that students use inappropriate tools to solve a problem, just to prove they can do it. In any case, if this is even possible using regular expressions -- and I don't think it is -- I have no idea how to do it. Good luck. Maybe somebody else might have a clue. I don't think it's possible because you don't know how many characters the string will have. Even if [\1-z] works (which it doesn't), you still have the same problem that you don't know where to stop: [a-z][\1-z][\2-z][\3-z][\4-z].**..[\99-z] This is related to the reason you can't parse indefinitely nested parentheses using a regular expression. -- Steven __**_ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/**mailman/listinfo/tutorhttp://mail.python.org/mailman/listinfo/tutor -- *Yours faithfully, Dinara Vakhitova* ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] cmd window
Hello, Excuse me for my stupid question, but I'm an absolute beginner in programming. I just would like to know what should I do in such case: when I run my program by clicking on the file itself, rather than from IDLE, the cmd window disappears just immediately after the output printing, so that I can't even see it. The solution must be very simple, but I can't find it in tutorials... Thank you! -- *Yours faithfully, Dinara Vakhitova* ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] cmd window
Thank you a lot, it helped! 2011/9/13 Wayne Werner waynejwer...@gmail.com On Tue, Sep 13, 2011 at 12:57 PM, Dinara Vakhitova di.marvell...@gmail.com wrote: Hello, Excuse me for my stupid question, but I'm an absolute beginner in programming. I just would like to know what should I do in such case: when I run my program by clicking on the file itself, rather than from IDLE, the cmd window disappears just immediately after the output printing, so that I can't even see it. The solution must be very simple, but I can't find it in tutorials... This is mainly because in later versions of Windows (XP onward, I believe) they changed a setting so that the command window would close after a program has executed. There are probably some more complicated settings to fix it on a global basis, but you'll find that most people add this line to the end of their programs: raw_input(Press Enter to continue) # Use input() with Python 3.x HTH, Wayne -- *Yours faithfully, Dinara Vakhitova* ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] cmd window
Thank you, Ramit and thank you, Alan! I have so many tutorials around me that I'm a little bit lost in the materials! Now I'll be focusing on Learning to Program by Alan Gauld. :) 2011/9/13 Alan Gauld alan.ga...@btinternet.com On 13/09/11 18:57, Dinara Vakhitova wrote: Hello, Excuse me for my stupid question, but I'm an absolute beginner in programming. Its not stupid and many have asked it before you :-) can't even see it. The solution must be very simple, but I can't find it in tutorials... You're obviously reading the wrong tutorials :-) It's covered in the Add a Little style topic in my tutorial. Its near the bottom of the page, in the box Note for Windows Users... It offers several different ways to deal with it depending on what you want to do. HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ __**_ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/**mailman/listinfo/tutorhttp://mail.python.org/mailman/listinfo/tutor -- *Yours faithfully, Dinara Vakhitova* ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor