2017-12-15 7 views
1

이 숙제 문제에 문제가 있습니다. 문자열에서 연속되는 "특수 기호"의 최대 수를 반환하십시오.

def symbol_count(s:str): -> int 
    """Return the largest number of consecutive "special symbols" in the 
    string s. 
    >>> symbol_count(’c0mput3r’) 
    0 
    >>> symbol_count(’H! [here’) 
    1 
    >>> symbol_count(’h3!!&o [email protected]#’) 
    3 
    """ 
    lst = [] 
    count = 0 
    for i in range(len(s)-1): 
     if s[i] in SPECIAL_SYMBOLS: 
      count +=1 
      if s[i+1] not in SPECIAL_SYMBOLS: 
       lst.append(count) 
       count = 0 
      else: 
       count += 1 
    if lst == []: 
     return 0 
    return max(lst) 

SPECIAL_SYMBOLS = '[email protected]#$%^&*()_+=[]?/'

그러나 마지막 예를 들어 나는 나의 수가이 경우 왜 궁금 0으로 다시 초기화되지 않은 추측 메신저 5 대신 3을 얻는다. 도와 주셔서 감사합니다.

답변

3

당신의 문제는 여기에 있습니다 :

else: 
    count += 1 

그것은 두 번 연속으로 특수 문자를 계산 끝납니다. 이 두 줄을 모두 제거하십시오.

+0

정말 고마워요! 정말로 나를 거기에서 구했다. – DWCY