Re: [Tutor] Hey guys!

2015-02-17 Thread Dave Angel

On 02/16/2015 11:22 PM, Levi Adissi wrote:

Thank you for using text email, rather than the html mail that so many 
newcomers use.



So I'm kind of stuck trying to program a function that returns a list of
tuples. The function takes 2 lists containing circles of which it should
compare list1[0] to list2[0] to see if they intersect. If they intersect or
touch then I should return them on a list of tuples(in the tuple would be
both intersecting circles).

I can't get circles_only to work the way I see it I'm comparing h to x only
if they're both in the same place on the list (hence my "h==x") I know it
doesn't work because the test returns None so I would really appreciate an
alternative method if you guys see one.

Here are my functions:


def circles_overlap(c1, c2):
x=(c2.center.y-c1.center.y)**2
y=(c2.center.x-c1.center.x)**2
distancemid=math.sqrt(x+y)
distancerad=(c1.radius+c2.radius)
if distancemid > distancerad:
return 1
elif distancemid < distancerad:
return -1
elif distancemid == distancerad:
return 0

def circles_only(lst1, lst2):
newlst=[]
for h in lst1:
   for x in lst2:
  if h==x:


That's silly.  You don't want to compare the two circles to see if 
they're equal.  Remove this line.



 if circles_overlap(lst1[h],lst2[x])== -1:


Why don't you tell us the exception this line causes?  lst1 is 
subscripted by integers, not by circle objects.


What you really want in this line is something like:
   if circles_overlap(h, x) ! = 1:
   newlst.append(h, x)



newlst.append(lst1[h],lst2[x])

 elif circles_overlap(lst1[h],lst2[x])== 0:
newlst.append(lst1[h],lst2[x])

print newlst


Don't print it, return it.  Otherwise, you're returning None.




TEST CASE:

 def test_circles_olap1(self):
 list1=[data_2.Circle(data_2.Point(2,3),
2),data_2.Circle(data_2.Point(2,3), 2), data_2.Circle(data_2.Point(2,3), 2)
]
 list2=[data_2.Circle(data_2.Point(6,3),
2),data_2.Circle(data_2.Point(10,3), 2), data_2.Circle(data_2.Point(5,3), 2)
]
 testor=functions_2.circles_only(list1,list2)
 newlist=[(data_2.Circle(data_2.Point(2,3),
2),data_2.Circle(data_2.Point(6,3), 2)),(data_2.Circle(data_2.Point(2,3),
2),data_2.Circle(data_2.Point(10,3), 2))]
 self.assertEqual(testor, newlist)



The test code makes no sense to me at all.  it's a method of some 
unspecified class, and it uses some namespaces called data_2  and 
functions_2 for an unknown purpose.




--
--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hey guys!

2015-02-17 Thread Alan Gauld

On 17/02/15 04:22, Levi Adissi wrote:

Hello. Thanks for your post.
However could you please use a subject that rflects the content. 
Otherwise we wind up with an archive full of "Hey Guys", "Hello chaps", 
"Gday mate", Bonjour" etc  Not very useful for searching.



So I'm kind of stuck trying to program a function that returns a list of
tuples.


Note that your function prints the list it does not return the list.
The two are very different.


The function takes 2 lists containing circles of which it should
compare list1[0] to list2[0] to see if they intersect. If they intersect or
touch then I should return them on a list of tuples(in the tuple would be
both intersecting circles).

I can't get circles_only to work the way I see it I'm comparing h to x only
if they're both in the same place on the list (hence my "h==x") I know it
doesn't work because the test returns None so I would really appreciate an
alternative method if you guys see one.

Here are my functions:


def circles_overlap(c1, c2):
x=(c2.center.y-c1.center.y)**2
y=(c2.center.x-c1.center.x)**2
distancemid=math.sqrt(x+y)
distancerad=(c1.radius+c2.radius)
if distancemid > distancerad:
return 1
elif distancemid < distancerad:
return -1
elif distancemid == distancerad:
return 0



Since this is a test you should probably just return True or False.
Either they overlap or they don't. If you want to use touching as a 
third result you can still give a boolean for the general case:


return
1 for overlap
-1 for touching
0 for not touching

1 and -1 both evaluate to True in a boolean sense
0 evaluates to False

This makes the test code in your second function look like:

if circles_overlap(c1,c2):
 newlst.append((c1,c2))

and you don't need the else clause


def circles_only(lst1, lst2):
newlst=[]
for h in lst1:
   for x in lst2:
  if h==x:


Why do you have this test? You only check if they overlap
when they are equal? That seems odd to me.
Also h and x seem an odd choice of name for two circles.
Why not

for c1 in lst1:
  for c2 in lst2:


 if circles_overlap(lst1[h],lst2[x])== -1:
newlst.append(lst1[h],lst2[x])


Now you are trying to use indexing to extract the circles
but you are (quite rightly) iterating over the lists directly
not using indexes. So h and x are circles, you don't need
to  get them out of the lists.

Also you are trying to store two items in your newlist rather
than a tuple. (See my suggested test above.)

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Hey guys!

2015-02-17 Thread Levi Adissi
So I'm kind of stuck trying to program a function that returns a list of
tuples. The function takes 2 lists containing circles of which it should
compare list1[0] to list2[0] to see if they intersect. If they intersect or
touch then I should return them on a list of tuples(in the tuple would be
both intersecting circles).

I can't get circles_only to work the way I see it I'm comparing h to x only
if they're both in the same place on the list (hence my "h==x") I know it
doesn't work because the test returns None so I would really appreciate an
alternative method if you guys see one.

Here are my functions:


def circles_overlap(c1, c2):
   x=(c2.center.y-c1.center.y)**2
   y=(c2.center.x-c1.center.x)**2
   distancemid=math.sqrt(x+y)
   distancerad=(c1.radius+c2.radius)
   if distancemid > distancerad:
   return 1
   elif distancemid < distancerad:
   return -1
   elif distancemid == distancerad:
   return 0

def circles_only(lst1, lst2):
   newlst=[]
   for h in lst1:
  for x in lst2:
 if h==x:
if circles_overlap(lst1[h],lst2[x])== -1:
   newlst.append(lst1[h],lst2[x])

elif circles_overlap(lst1[h],lst2[x])== 0:
   newlst.append(lst1[h],lst2[x])

   print newlst


TEST CASE:

def test_circles_olap1(self):
list1=[data_2.Circle(data_2.Point(2,3),
2),data_2.Circle(data_2.Point(2,3), 2), data_2.Circle(data_2.Point(2,3), 2)
   ]
list2=[data_2.Circle(data_2.Point(6,3),
2),data_2.Circle(data_2.Point(10,3), 2), data_2.Circle(data_2.Point(5,3), 2)
   ]
testor=functions_2.circles_only(list1,list2)
newlist=[(data_2.Circle(data_2.Point(2,3),
2),data_2.Circle(data_2.Point(6,3), 2)),(data_2.Circle(data_2.Point(2,3),
2),data_2.Circle(data_2.Point(10,3), 2))]
self.assertEqual(testor, newlist)


Thanks in advance!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor