1.設(shè)計(jì)一個(gè)函數(shù),統(tǒng)計(jì)一個(gè)字符串中出現(xiàn)頻率最高的字符(單個(gè)符號(hào))及其出現(xiàn)次數(shù)
#利用字典的key值唯一這個(gè)特點(diǎn)把字符串的每一個(gè)字符作為key,次數(shù)作為value
{字符:次數(shù)}
def get_count(str1):
count_dict={}
for i in str1:
count = count_dict.get(i,0)
count += 1
count_dict [i] = count
max=0
char=''
for key in count_dict:
value = count_dict[key]
if value > max:
max = value
char = key
return char,max
get_count('abcaabc')
第2題:
點(diǎn)(Point)類: 擁有屬性x坐標(biāo)和y坐標(biāo)。擁有功能是計(jì)算兩個(gè)點(diǎn)之間的距離。
線段(Line)類: 擁有屬性起點(diǎn)和終點(diǎn)。功能有:1.獲取線段的長(zhǎng)度 2.判斷指定的點(diǎn)是否在該線段上。
class Point():
def __init__(self,x=0,y=0):
self.x=x
self.y=y
def distance(self,other):
return ((self.x-other.x)**2+(self.y-other.y)**2)**0.5
class Line():
def __init__(self,start_point,end_point):
self.start_point=start_point
self.end_point=end_point
def length_distance():
#線段的長(zhǎng)度就是線段的起點(diǎn)到終點(diǎn)的距離
return self.start_point.distance(self.end_point)
def is_on_line(self,point1):
distance1 = self.start_point.distance(point1)
distance2 = self.end_point.distance(point1)
if distance1+distance2 == self.length():
return True
return False