Posts: 19
Threads: 8
Joined: Mar 2017
Nov-26-2017, 03:54 AM
(This post was last modified: Nov-26-2017, 03:54 AM by Miraclefruit.)
According to the solutions, the following code prints out
Square with side 5.0
Rectangle with area 6.0
Circle with radius 2.0
Square with side 6.0
Rectangle with area 6.0
Circle with radius 1.0
class Shape(object):
def __eq__(s1, s2):
return s1.area() == s2.area()
def __lt__(s1, s2):
return s1.circum() < s2.circum()
class Rectangle(Shape):
def __init__(self, h, w):
self.height = float(h)
self.width = float(w)
def circum(self):
return 2*(self.height + self.width)
def __str__(self):
return 'Rectangle with area ' + str(self.height*self.width)
class Square(Rectangle):
def __init__(self, s):
Rectangle.__init__(self, s, s)
def __str__(self):
return 'Square with side ' + str(self.height)
class Circle(Shape):
def __init__(self, radius):
self.radius = float(radius)
def circum(self):
return 3.14159*(2*self.radius)
def __lt__(self, other):
return self.radius < other.radius
def __str__(self):
return 'Circle with diameter ' + str(2.0*self.radius)
def reorder(L):
for e in L:
if e < L[0]:
L[0] = e
L = [Square(6), Rectangle(2, 3), Circle(1)]
try:
reorder(L)
for e in L:
print(e)
except:
for e in L:
print(e ) Could someone care to explain how that's the solution? Thank you
Posts: 606
Threads: 3
Joined: Nov 2016
You have to step through the code step-by-step
with the help of a debugger.
Posts: 8,160
Threads: 160
Joined: Sep 2016
There is no way this code could produce that output - the L list has only 3 objects in it if not for anything else. There are number of other problems, incl. with the way try/except is used.
Posts: 606
Threads: 3
Joined: Nov 2016
Nov-26-2017, 10:58 AM
(This post was last modified: Nov-26-2017, 10:58 AM by heiner55.)
This is the outout:
Output: Rectangle with area 6.0
Rectangle with area 6.0
Circle with diameter 2.0
Posts: 544
Threads: 15
Joined: Oct 2016
Nov-26-2017, 10:51 PM
(This post was last modified: Nov-26-2017, 11:03 PM by Windspar.)
1. Put some line space in code. My eyes hurt from reading that.
class Shape(object):
def __eq__(s1, s2): # ==
return s1.area() == s2.area()
def __lt__(s1, s2): # <
return s1.circum() < s2.circum()
class Rectangle(Shape):
def __init__(self, h, w):
self.height = float(h)
self.width = float(w)
def circum(self):
return 2*(self.height + self.width)
def __str__(self): # use for print
return 'Rectangle with area ' + str(self.height*self.width)
class Square(Rectangle):
def __init__(self, s):
Rectangle.__init__(self, s, s)
def __str__(self):
return 'Square with side ' + str(self.height)
class Circle(Shape):
def __init__(self, radius):
self.radius = float(radius)
def circum(self):
return 3.14159*(2*self.radius)
def __lt__(self, other):
return self.radius < other.radius
def __str__(self):
return 'Circle with diameter ' + str(2.0*self.radius)
def reorder(L):
for e in L:
if e < L[0]: # only compares to first value
L[0] = e # only changes first value
if __name__ == '__main__':
L = [Square(6), Rectangle(2, 3), Circle(1)]
try:
reorder(L)
for e in L:
print(e)
except:
for e in L:
print(e ) 2. Would need to know example to be sure. I would say you mess up reorder function.
L[0] = e # only replace first item
Another thing I notice why does Shape compare area. When no object have an area method ?
99 percent of computer problems exists between chair and keyboard.
Posts: 19
Threads: 8
Joined: Mar 2017
Nov-27-2017, 01:31 AM
(This post was last modified: Nov-27-2017, 01:31 AM by Miraclefruit.)
This is one of the quiz solutions from the MIT courseware. So I'm really confused as to how they got that.
(Nov-26-2017, 10:58 AM)heiner55 Wrote: This is the outout:
Output: Rectangle with area 6.0
Rectangle with area 6.0
Circle with diameter 2.0
Could you explain how thats the output? I understand what the code is doing until the reorder function
Posts: 12,031
Threads: 485
Joined: Sep 2016
Nov-27-2017, 01:43 AM
(This post was last modified: Nov-27-2017, 01:44 AM by Larz60+.)
To really understand how this code works, load it up with a debugger and
single step through the entire process, looking at all the variables along
the way.
I've done a lot of reverse engineering of code, often at the machine code
level. In which case, I would single step through a routine, checking all
registers, memory, etc.
Debuggers help you to do the same with higher level languages.
I don't knoe if you use an IDE or not, but most come with built in debuggers.
If not, do a google search on "Python IDE's" and find one that does.
Personally, I like PyCharm, but that's strictly a personal thing. There are many good
IDE's out there.
Posts: 19
Threads: 8
Joined: Mar 2017
(Nov-27-2017, 01:43 AM)Larz60+ Wrote: To really understand how this code works, load it up with a debugger and
single step through the entire process, looking at all the variables along
the way.
I've done a lot of reverse engineering of code, often at the machine code
level. In which case, I would single step through a routine, checking all
registers, memory, etc.
Debuggers help you to do the same with higher level languages.
I don't knoe if you use an IDE or not, but most come with built in debuggers.
If not, do a google search on "Python IDE's" and find one that does.
Personally, I like PyCharm, but that's strictly a personal thing. There are many good
IDE's out there.
Thank you! I used http://pythontutor.com/visualize.html#mode=display and it helped me understand what each line is doing.
Posts: 544
Threads: 15
Joined: Oct 2016
Really Mit quiz courseware. I thought they be harder.
Just walk through the code.
def reorder(L):
for e in L: #e will be Square then Rectangle, and finally a Circle
if e < L[0]: # e always compares to Square. You are compare __lt__ overload.
# round 1 Square < Square = False
# round 2 Rectangle < Square = True
# round 3 Circle < Square = False
L[0] = e # Square get Replace by Rectangle
99 percent of computer problems exists between chair and keyboard.
Posts: 606
Threads: 3
Joined: Nov 2016
Nov-27-2017, 05:13 AM
(This post was last modified: Nov-27-2017, 05:14 AM by heiner55.)
You can try IDE "spyder3" if you are on a Linux system,
because on my system it was already installed.
(Sypder3 is a simple IDE)
|