第1题
class Athlete:
def __init__(self,a_name,a_dob=None,a_times=[]):
self.name = a_name
self.dob = a_dob
self.times = a_times
def top3(self):
return sorted(set([self.sanitize(t) for t in self.times]))[0:3]
def sanitize(self,time_string):
if '-' in time_string:
splitter = '-'
elif ':' in time_string:
splitter = ':'
else:
return (time_string)
(mins,secs) = time_string.split(splitter)
return (mins+'.'+secs)
class Rugby(Athlete):
def __init__(self,a_name,a_dob,a_squat,a_times):
Athlete.__init__(self, a_name,a_dob,a_times)
self.squat=a_squat
第2题
class OtherAthlete(Athlete):
def __init__(self,a_name,a_bod,a_squat,a_times):
Athlete.__init__(self, a_name,a_bod,a_times)
self.squat=a_squat
def top3(self):
return sorted([self.sanitize(t) for t in self.times])[0:3]
第3题
def get_coach_data(filename):
with open(filename) as f:
line = f.readline()
return line.strip().split(',')
loren = get_coach_data('mywork/loren.txt')
mark = get_coach_data('mywork/mark.txt')
loren = Rugby(loren.pop(0),loren.pop(0),loren.pop(0),loren)
mark = OtherAthlete(mark.pop(0),mark.pop(0),mark.pop(0),mark)
def print_rugby(athlete):
print(athlete.name)
print(athlete.dob)
print(athlete.squat)
print(athlete.top3())
print_rugby(loren)
print_rugby(mark)
2011-11-3
270
3.59
['3.11', '3.23', '4.10']
mark
2010-2-4
300
['3.11', '3.11', '3.23']
第4题
class Father():
def __init__(self):
self.color = 'black'
def talk(self):
print("---爸爸的表达能力---")
class Mother():
def __init__(self):
self.height = 170
def smart(self):
print("---妈妈聪明的头脑---")
class Child(Father, Mother):
def __init__(self):
Father.__init__(self)
Mother.__init__(self)
child= Child()
child.smart()
print(child.color)
---妈妈聪明的头脑---
black