1.设计一个函数,统计一个字符串中出现频率最高的字符(单个符号)及其出现次数
#利用字典的key值唯一这个特点把字符串的每一个字符作为key,次数作为value
{字符:次数}
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题:
点(Point)类: 拥有属性x坐标和y坐标。拥有功能是计算两个点之间的距离。
线段(Line)类: 拥有属性起点和终点。功能有:1.获取线段的长度 2.判断指定的点是否在该线段上。
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():
#线段的长度就是线段的起点到终点的距离
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
网友评论