test

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容