def get_len_of_str(string):
i=0
my_list=[]
my_number=[]
# 빈 문자열일 때 0 반환
if string == '' :
return 0
else :
while i < len(string):
# 리스트의 마지막일 때
if i == len(string)-1 :
# 중복되는 알파벳이 있는 경우
if string[i] in my_list :
n=len(my_list)
my_number.append(n)
break
# 중복되는 알파벳이 없는 경우
else :
my_list.append(string[i])
n=len(my_list)
my_number.append(n)
break
# 중복되는 알파벳이 있는 경우
elif string[i] in my_list:
n=len(my_list)
my_number.append(n)
my_list=[]
# 중복되는 알파벳이 없는 경우
else :
my_list.append(string[i])
i=i+1
my_number.sort()
return my_number[-1]
알파벳을 처음부터 돌며 확인 --> my_list로 알파벳 중복 체크
my_number 리스트로 가장 큰 숫자 확인
중복되는 알파벳이 없는 경우 --> my_list 에 추가하기
중복되는 알파벳이 있는 경우 --> my_number에 값 추가하고 break
def get_len_of_str(str):
str_second = []
str_length = 0
for new_str in str:
# 중복되는 알파벳이 있는 경우
if new_str in str_second:
# str_second 삭제
del str_second[:]
str_second.append(new_str)
# 중복되는 알파벳이 없는 경우
else:
str_second.append(new_str)
# 알파벳 길이 비교
if len(str_second) > str_length:
str_length = len(str_second)
return str_length
1. list.clear()
>>> my_list = [1,2,3,4,5]
>>> my_list.clear()
>>> my_list
[]
2. del list[:]
>>> my_list = [1,2,3,4,5]
>>> del my_list[:]
>>> my_list
[]
def get_len_of_str(s):
dct = {}
max_so_far = curr_max = start = 0
for index, i in enumerate(s):
if i in dct and dct[i] >= start:
max_so_far = max(max_so_far, curr_max)
curr_max = index - dct[i]
start = dct[i] + 1
else:
curr_max += 1
dct[i] = index
return max(max_so_far, curr_max)
리스트의 값을 돌며 count를 실행해 주는 methods
start값이 있는 경우, start값 부터 count
예시 1) start 생략한 경우
>>> my_list = ['apple', 'banana', 'grapes', 'pear']
>>> for count,i in enumerate(my_list):
... print(count, i)
...
0 apple
1 banana
2 grapes
3 pear
>>> my_list = ['apple', 'banana', 'grapes', 'pear']
>>> for count,i in enumerate(my_list):
... print(i, count)
...
apple 0
banana 1
grapes 2
pear 3
예시 2) start 생략하지 않은 경우
>>> my_list = ['apple', 'banana', 'grapes', 'pear']
>>> for count,i in enumerate(my_list, 100):
... print(count, i)
...
100 apple
101 banana
102 grapes
103 pear