Coordinate Grid Points

James Stroud jstroud at mbi.ucla.edu
Mon Feb 5 21:33:18 EST 2007


Eric.Gabrielson at gmail.com wrote:
> On Feb 5, 3:29 pm, James Stroud <jstr... at mbi.ucla.edu> wrote:
> 
>>Eric.Gabriel... at gmail.com wrote:
>>
>>>Hello,
>>>       I am very knew to python and am attempting to write a program
>>>in python that a friend of mine is having to write in java. I am doing
>>>this for fun and would like some help as to how i can generate random
>>>coordinate points (x,y) and compare them with user inputted coordinate
>>>points. For example how will I be able to access the separate values,
>>>the x from the x,y position. I think I understand everything except
>>>1)the random coordinate points 2) the getting the users inputted x and
>>>y values in one line ("Please guess a coordinate point from (1,1) to
>>>(20,20): ") as opposed to  ("Please enter an x value:" and "Please
>>>enter a y value") and finally 3) acessing the x value from the x,y
>>>coordinate function. the assignment description is located here http://
>>>www.cs.washington.edu/education/courses/142/07wi/homework/
>>>homework.html if you would like to see a more in depth discription of
>>>the game.
>>
>>>Many Thanks,
>>>Eric
>>
>>For 1: see the random module (e.g. random.randint)
>>For 2: see the "eval" function and "raw input"
>>For 2 (without cheating): see the re module. For example:
>>*****************************************************************************
>> ***map(int, re.compile("\(?(\d+),(\d+)\)?").search(inpt).groups())****
>>*****************************************************************************
>>(Giving you the latter because eval will do this for you anyway.)
>>
>>Also see "raw_input".
>>
>>James
> 
> 
> Thank you very much for your response i will play around with the code
> when I have some time ( hopefully later tonight) but could you please
> breakdown what the part that I surrounded with asterisks, I think I
> recognize it but don't understand it.
> 
> Eric
> 

"re" is the regular expression module for python. "re.compile()" 
compiles the regular expression into a regular expression object with 
certain attributes, one of which is "search". "search" searches a 
string, here "inpt", and this produces a "match" (actually a 
_sre.SRE_Match) object that has, as one of its attributes, a "groups()" 
method that returns matches to the grouped expressions inside the 
regular expression, i.e. expressions surrounded by un-escaped parentheses.

Inside of the quotes is a regular expression string, (that, in general, 
should be preceded immediately by a lowercase r--but is not here because 
it doesn't matter in this case and I forgot to). map() maps a callable 
(here int) to the list of groups returned by groups(). Each group is a 
string matching r"\d+" which is an expression for one or more digits. 
Each group is converted into an integer and map returns a list of 
integers. I escaped the enclosing parentheses and put question marks 
(match zero or one) so that the enclosing parentheses would be optional. 
This makes all of the following evaluate to [20, 20]:

      "20,20"
      "(20,20"
      "20,20)"
      "(20,20)"

Except for typos, the middle two would be quite uncommon for user input, 
but would match the expression. Note also, that I didn't allow for 
whitespace anywhere, which might be expected. Arbitrary whitespace is 
matched by r"\s*".

James



More information about the Python-list mailing list