-
请用Python程式或你擅长的语言写出下面问题的答案
(1).一个不固定长度的字串,找出连续最长的0或连续最长的1
[提示1]: "1110001111" 其中连续3个1与3个0与4个1 --->程式反馈结果要是最长的是4个1
[提示2]: "1010001010" ---> 程式反馈结果最长的是3个0
[提示3]: 题目问的是不固定长度(N)的一组字串,ex:"len(010101011100011111110001010111111...) isN"
def findMaxLenForSameChar(strValue): index, count, list = 0, 0, [] if len(strValue) == 1: return (count + 1, strValue[index]) else: for i in range(1, len(strValue)): if strValue[index] == strValue[i]: index, count = index + 1, count + 1 else: list.append((count + 1, strValue[index])) index, count = index + 1, 0 if i == len(strValue) - 1: list.append((count + 1, strValue[index])) return max(list) return list
(2).有一个字串 "I will go to Jinan and see a move !!!",请用寫程式 将他反转输出为"!!! move a see and Jinan to go will I"
def reverseWorld(strValue): import re revwords = re.split(r'(\s+)', strValue) revwords.reverse() return ''.join(revwords)