Recitation 6b (Dec 6, 2011)
Welcome (Fall 2011/2012) » Recitation Logs (Fall 2011/2012) » Recitation 6b (Dec 6, 2011)
Table of Contents
Note: this recitation covers the material of recitation 5a.
Code examples
Example 1: plane geometry classes
class Point: """ Represents a point (x, y) in the plane. """ def is_origin(self): return self.x == self.y == 0 ## def make(self2, x, y): ## self2.x = x ## self2.y = y def __init__(self, x, y): self.x = x self.y = y def __repr__(self): ## return "Point {} {}".format(self.x, self.y) ## return "Point({}, {})".format(self.x, self.y) return "Point({0.x}, {0.y})".format(self) def __eq__(self, other): return isinstance(other, Point) and \ self.x == other.x and self.y == other.y class Line: """ Represents a straight line in the plane, using the equation ax + by = c. """ def __init__(self, a,b,c): self.a = a self.b = b self.c = c def __repr__(self): return "Line({a}, {b}, {c})".format(**self.__dict__) def is_vertical(self): return self.b == 0 def is_horizontal(self): return self.a == 0 def is_parallel_to(self, other): return self.a*other.b == self.b*other.a ## def is_equal_to(self, other): def __eq__(self, other): return isinstance(other, Line) and \ self.is_parallel_to(other) and \ self.a*other.c == self.c*other.a and \ self.c*other.b == self.b*other.c def intersect(self, other): assert not self.is_parallel_to(other) # solve a linear system of 2 equations in 2 variables # using Cramer's rule det = self.a * other.b - self.b * other.a detx = self.c * other.b - self.b * other.c dety = self.a * other.c - self.c * other.a return Point(detx/det, dety/det) def make_line_through(p, q): assert p != q if p.x == q.x: # use representation x = c return Line(1, 0, p.x) else: # use representation y = mx + n m = (p.y-q.y)/(p.x-q.x) n = p.y-m*p.x return Line(-m, 1, n)
Download (right click -> save link as "ex1.py")
Interpreter session log
Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> def add_points(x1,y1,z1,x2,y2,z2):
return x1+x2,y1+y2,z1+z2
>>> #(ID, name, grades_list)
>>> s{'id':012345, 'name': "Moshe", grades_list=[100,100,98]}
SyntaxError: invalid syntax
>>> s={'id':012345, 'name': "Moshe", grades_list=[100,100,98]}
SyntaxError: invalid token
>>> s={'id':12345, 'name': "Moshe", grades_list:[100,100,98]}
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
s={'id':12345, 'name': "Moshe", grades_list:[100,100,98]}
NameError: name 'grades_list' is not defined
>>> s={'id':12345, 'name': "Moshe", 'grades_list':[100,100,98]}
>>> s['name']
'Moshe'
>>> def calc_averge(s):
return sum(s['grades_list'])/len(s['grades_list'])
>>> def calc_average_for_student(s):
return sum(s['grades_list'])/len(s['grades_list'])
>>> calc_average_for_student(s)
99.33333333333333
>>> s={'id':12345, 'name': "Moshe", 'grades_list':[100,100,98], 'calc_average':calc_average_for_student}
>>> s['calc_average'](s)
99.33333333333333
>>> #L=[square, circle,...]
>>> #sum(area(x) for x in L)
>>> #sum(x['area'](x) for x in L)
>>> L = [1,2]
>>> list.append(L, 6)
>>> L
[1, 2, 6]
>>> L.append(8)
>>> L
[1, 2, 6, 8]
>>> str.join
<method 'join' of 'str' objects>
>>> str.join(", ", ["Hello", "World"])
'Hello, World'
>>> ", ".join(["Hello", "World"])
'Hello, World'
>>> ================================ RESTART ================================
>>>
>>> Point
<class '__main__.Point'>
>>> help(Point)
Help on class Point in module __main__:
class Point(builtins.object)
| Represents a point (x, y) in the plane.
|
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
>>> p = Point()
>>> p.x = 10
>>> p.y = 50
>>> p.y
50
>>> Complex(5, 2)
Traceback (most recent call last):
File "<pyshell#35>", line 1, in <module>
Complex(5, 2)
NameError: name 'Complex' is not defined
>>> complex(5, 2)
(5+2j)
>>> x=_
>>> x.real
5.0
>>> x.imag
2.0
>>> def make_point(x, y):
p = Point()
p.x = x
p.y = y
return p
>>> make_point(10,20).y
20
>>> ================================ RESTART ================================
>>>
>>> p=make_point(10,20)
Traceback (most recent call last):
File "<pyshell#47>", line 1, in <module>
p=make_point(10,20)
NameError: name 'make_point' is not defined
>>> def make_point(x, y):
p = Point()
p.x = x
p.y = y
return p
>>> p=make_point(10,20)
>>> p.is_origin()
False
>>> Point.is_origin(p)
False
>>> make_point(0,0).is_origin()
True
>>> ================================ RESTART ================================
>>>
>>> p = Point(); p.make(10,20)
>>> p.x
10
>>> []
[]
>>> list()
[]
>>> ================================ RESTART ================================
>>>
>>> Point()
Traceback (most recent call last):
File "<pyshell#58>", line 1, in <module>
Point()
TypeError: __init__() takes exactly 3 arguments (1 given)
>>> Point(10,30).y
30
>>> ================================ RESTART ================================
>>>
>>> p=Point(10,20)
>>> p.make(60,70)
>>> Point.make(p, 60, 70)
>>> ================================ RESTART ================================
>>>
>>> p=Point(10,20)
>>> p.make(60,70)
>>> p.x
60
>>> p.bla = 10
>>> p.bla = "hi"
>>> P.not_existing
Traceback (most recent call last):
File "<pyshell#68>", line 1, in <module>
P.not_existing
NameError: name 'P' is not defined
>>> p.not_existing
Traceback (most recent call last):
File "<pyshell#69>", line 1, in <module>
p.not_existing
AttributeError: 'Point' object has no attribute 'not_existing'
>>> p.__dict__
{'y': 70, 'x': 60, 'bla': 'hi'}
>>> p.is_origin
<bound method Point.is_origin of <__main__.Point object at 0x0152C2F0>>
>>> Point(6,7)
<__main__.Point object at 0x0152CA10>
>>> p
<__main__.Point object at 0x0152C2F0>
>>> id(p)
22201072
>>> hex(id(p))
'0x152c2f0'
>>> def show_point(p):
print("Point", p.x, p.y")
SyntaxError: EOL while scanning string literal
>>>
>>> def show_point(p):
print("Point", p.x, p.y)
>>> show_point(p)
Point 60 70
>>> def show_point(p):
print("Point", p.x, p.y, "!")
>>> show_point(p)
Point 60 70 !
>>> p
<__main__.Point object at 0x0152C2F0>
>>> ================================ RESTART ================================
>>>
>>> Point(60,70)
Point 60 70
>>> ================================ RESTART ================================
>>>
>>> Point(60,70)
Point(60, 70)
>>> p=Point(60,70)
>>> p
Point(60, 70)
>>> Point(60, 70)
Point(60, 70)
>>> ================================ RESTART ================================
>>>
>>> Point(60, 70)
Traceback (most recent call last):
File "<pyshell#91>", line 1, in <module>
Point(60, 70)
File "C:/Documents and Settings/ranihod/Desktop/r6-ex1.py", line 20, in __repr__
return str(self.x, self.y)
TypeError: str() argument 2 must be str, not int
>>> ================================ RESTART ================================
>>>
>>> Point(60, 70)
(60, 70)
>>> ================================ RESTART ================================
>>>
>>> Point(60, 70)
Point(60, 70)
>>> ================================ RESTART ================================
>>>
>>> Point(60, 70)
Point(60, 70)
>>> ================================ RESTART ================================
>>>
>>> Point(5,5)
Point(5, 5)
>>> p=Point(60,70)
>>> p.z= 10
>>> ================================ RESTART ================================
>>>
>>> Line(1,2,3).is_vertical()
False
>>> Line(1,0,35).is_vertical()
True
>>> ================================ RESTART ================================
>>>
>>> Line(1,2,3).is_parallel_to(Line(5,6,7))
False
>>> Line(1,2,3).is_parallel_to(Line(5,10,7))
True
>>> ================================ RESTART ================================
>>>
>>> Line(1,2,3).is_parallel_to(Line(5,10,7))
True
>>> Line(1,2,3).is_equal_to(Line(5,10,7))
False
>>> Line(1,2,3).is_equal_to(Line(5,10,15))
True
>>> Line(1,2,3) == Line(5,10,15)
False
>>> ================================ RESTART ================================
>>>
>>> Line(1,2,3) == Line(5,10,15)
True
>>> Line(1,2,3) == Line(5,10,20)
False
>>> 6 == 7
False
>>> Line(1,2,3) == 6
Traceback (most recent call last):
File "<pyshell#109>", line 1, in <module>
Line(1,2,3) == 6
File "C:/Documents and Settings/ranihod/Desktop/r6-ex1.py", line 47, in __eq__
return self.is_parallel_to(other) and \
File "C:/Documents and Settings/ranihod/Desktop/r6-ex1.py", line 43, in is_parallel_to
return self.a*other.b == self.b*other.a
AttributeError: 'int' object has no attribute 'b'
>>> type(5)
<class 'int'>
>>> type(Line(5,6,7))
<class '__main__.Line'>
>>> type(Point(5\,7))
SyntaxError: unexpected character after line continuation character
>>> type(Point(5,7))
<class '__main__.Point'>
>>> type(max)
<class 'builtin_function_or_method'>
>>> ================================ RESTART ================================
>>>
>>> Line(1,2,3).intersect(Line(4,5,60))
Point(35.0, -16.0)
>>> 1*35+2*-16==3
True
>>> 4*35+5*-16==60
True
>>> class Bla:
pass
>>> b=Bla()
>>> b.x = 10; b.y=20
>>> Point.is_origin(b)
False
>>> b.x=b.y=0
>>> Point.is_origin(b)
True
>>> b.is_origin()
Traceback (most recent call last):
File "<pyshell#126>", line 1, in <module>
b.is_origin()
AttributeError: 'Bla' object has no attribute 'is_origin'
>>> ================================ RESTART ================================
>>>
>>> make_line_through(Point(10,10), Point(10, 20))
Traceback (most recent call last):
File "<pyshell#127>", line 1, in <module>
make_line_through(Point(10,10), Point(10, 20))
File "C:/Documents and Settings/ranihod/Desktop/r6-ex1.py", line 64, in make_line_through
m = (p.y-q.y)/(p.x-q.x)
ZeroDivisionError: division by zero
>>> ================================ RESTART ================================
>>>
>>> make_line_through(Point(10,10), Point(10, 20))
Line(1, 0, 10)
>>> make_line_through(Point(10,10), Point(20, 20))
Line(-1.0, 1, 0.0)
>>> p = Point(1,2,3); q=Point(4,7,8); r=Point(3,2, -5)
Traceback (most recent call last):
File "<pyshell#130>", line 1, in <module>
p = Point(1,2,3); q=Point(4,7,8); r=Point(3,2, -5)
TypeError: __init__() takes exactly 3 positional arguments (4 given)
>>> p = Point(3,2); q=Point(7,8); r=Point(3, -5)
>>>
>>> make_line_through(p,q)
Line(-1.5, 1, -2.5)
>>> make_line_through(p,r)
Line(1, 0, 3)
>>> l1=make_line_through(p,q)
>>> l2=make_line_through(p,r)
>>> l1.intersect(l2)
Point(3.0, 2.0)
>>> l1.intersect(l2) == p
False
>>> ================================ RESTART ================================
>>>
>>> p = Point(3,2); q=Point(7,8); r=Point(3, -5)
>>> l1=make_line_through(p,q)
>>> l2=make_line_through(p,r)
>>> l1.intersect(l2) == p
True
>>> p = Point(3,2); q=Point(7,8); r=Point(3, -56)
>>> l1=make_line_through(p,q)
>>> l2=make_line_through(p,r)
>>> l1.intersect(l2) == p
True
>>> l1.intersect(l2)
Point(3.0, 2.0)
>>> p = Point(3.55,2); q=Point(7,8); r=Point(3, -56)
>>> l1=make_line_through(p,q)
>>> l2=make_line_through(p,r)
>>> l1.intersect(l2)
Point(3.55, 2.0)
>>> l1.intersect(l2) == p
True
>>> p = Point(3.55,2); q=Point(7,8.23); r=Point(3, -56.1)
>>> l1=make_line_through(p,q)
>>> l2=make_line_through(p,r)
>>> l1.intersect(l2)
Point(3.55, 1.9999999999999996)
>>> l1.intersect(l2) == p
False
>>>