加拿大瑞爾森大學(xué) Python習(xí)題 Lab 8
s?again.
?
3. ??Write ?a ?class?called?Circle.?The?Circle?class?has?an?instance?variable?called?radius,?and?it?should be initialized with an input?radius, i.e.,?the?method?__init__?accepts?a?number?called?radius. Circle should also?have a method that computes the?circumference.
4. ??Build?a list?of length?1000,?called?circles,?that?contains?circle?objects?of random?radii using?the ?random module.?Write?a?function?called ?circle_cmp that?compares?two?input?circles?based?on?their?circumference. Write?a?new bubble?sort?algorithm?that?accepts?an?extra?input?that?is?a function. The new bubble?sort?algorithm?uses?the?input?function?to?compare?circles.?Use this new version of?the bubble sort to sort circles?using the circle_cmp?function.
5. ??Implement the __lt__?function for the Circle?class?to?accomplish?circle?comparison?based?on?circumference.
6. ??Build?a new list?of circles, like?in?Question?4, but?now?the?circles?in?the?list?have?the?__lt__ function. Use the original bubble?sort algorithm?to?sort this?new?list.?The?original?bubble?sort?algorithm should work for this?list of?circles without any change.
代碼
8-1 Bubble Sort