[Python] Checkio Solution 解答 – Long Repeat

架網編程

Problem:

This mission is the first one of the series. Here you should find the length of the longest substring that consists of the same letter. For example, line "aaabbcaaaa" contains four substrings with the same letters "aaa", "bb","c" and "aaaa". The last substring is the longest one which makes it an answer.

 

Level:

Elementary

 

Input:

String.

 

Output:

Int.

 

Source: https://py.checkio.org


My Solution:

def long_repeat(line): 
  c = [] 
  if len(line) == 1: 
    longest = 1 
  else: 
    for i in range(len(line)-1): 
      if line[i] != line[i+1]: 
        c.append(1) 
      elif i == 0: 
        c.append(1) 
        c[-1] += 1 
      else: 
        c[-1] += 1 
      i += 1 
    if len(c) == 1: 
      longest = c[0] 
    elif len(c) == 0: 
      longest = 0 
    else: 
      longest = max(c) 
  return longest

Comment:

The Checkio system even check your code using empty string. Sometimes when we solve a problem, we do need to consider different situations.

發佈日期:2017年9月12日 | 作者:橘小佑

Copyright © Vantopics