做网站的组要具备哪些素质,手机表白网页制作,网站备案 太烦,网站建设 考核指标CheckiO 是面向初学者和高级程序员的编码游戏#xff0c;使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务#xff0c;从而提高你的编码技能#xff0c;本博客主要记录自己用 Python 在闯关时的做题思路和实现代码#xff0c;同时也学习学习其他大神写的代码。
Chec…
CheckiO 是面向初学者和高级程序员的编码游戏使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务从而提高你的编码技能本博客主要记录自己用 Python 在闯关时的做题思路和实现代码同时也学习学习其他大神写的代码。
CheckiO 官网https://checkio.org/
我的 CheckiO 主页https://py.checkio.org/user/TRHX/
CheckiO 题解系列专栏https://itrhx.blog.csdn.net/category_9536424.html
CheckiO 所有题解源代码https://github.com/TRHX/Python-CheckiO-Exercise 题目描述
【Long Repeat】给定一个字符串找到字符串中最长的相同字符重复出现的次数并返回它的重复次数。例如字符串“aaabbcaaaa”包含具有相同字母“aaa”“bb”“c”和“aaaa”的四个子字符串。 最后一个子字符串是最长的一个字符串你应该返回 4 。
【链接】https://py.checkio.org/mission/long-repeat/
【输入】一个字符串
【输出】一个整数
【范例】
long_repeat(sdsffffse) 4
long_repeat(ddvvrwwwrggg) 3解题思路
遍历字符串如果前一个字符与后一个字符相同就将其出现的次数加一同时每次遍历都用 max() 方法记录字符出现次数最多的
代码实现
def long_repeat(line: str) - int:new_string num 0max_num 0for string in line:if string ! new_string:new_string stringnum 1else:num 1max_num max(max_num, num)return max_numif __name__ __main__:# These asserts using only for self-checking and not necessary for auto-testingassert long_repeat(sdsffffse) 4, Firstassert long_repeat(ddvvrwwwrggg) 3, Secondassert long_repeat(abababaab) 2, Thirdassert long_repeat() 0, Emptyprint(Run is good. How is Check?)大神解答 大神解答 NO.1 from itertools import groupbydef long_repeat(line):return max((sum(1 for _ in g) for k, g in groupby(line)), default0)大神解答 NO.2 def long_repeat(line):m0p0for x in range(len(line)):if xlen(line)-1:if line[x]line[x1]:p1else:p0if p1m:mp1return m