编程

写一个函数,统计字符串中的出现最高次数的字符,输出该字符,以及次数


def str_num(str1: str):
    num = 0
    num_i = ''
    for i in str1:
        if num < str1.count(i):
            num = str1.count(i)
            num_i = i
    return num, num_i


print(str_num('strrinnnnngggg'))

声明一个类Point具有x,y坐标.功能是计算两点之间的距离

声明一个线段Line 具有start点end点,功能有计算线段长度,判断一个点是否在这个线段上

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def distance(self, other):
        return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5


p1 = Point(1, 1)
p2 = Point(3, 3)


class Line:
    def __init__(self, start, end):
        self.start = start
        self.end = end

    def length(self):
        return ((self.start.x - self.end.x) ** 2 + (self.end.y - self.end.y) ** 2) ** 0.5

    # (y-y1)/(y2-y1)=(x-x1)/(x2-x1)  y = kx +b 斜率 = (y1-y2)/(x1-x2)
    def in_line(self, other: Point):
        if (other.y - self.start.y) / (self.end.y - self.start.y) == (other.x - self.start.x) / (
                self.end.x - self.start.x):
            if (self.start.y - self.end.y) / (self.start.x - self.end.x) > 0:
                if self.end.y > other.y > self.start.y and self.end.x > other.x > self.start.x:
                    print('点在线段上')
                else:
                    print('点不在线段上')
            else:
                if self.start.y > other.y > self.end.y and self.end.x > other.x > self.start.x:
                    print('点在线段上')
                else:
                    print('点不在线段上')
            print('点在直线上')
        else:
            print('点不在直线上')


l1 = Line(p1, p2)
p3 = Point(5, 5)
l1.in_line(p3)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容