File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed
Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change 1+ # 제너레이터에 사용할 함수
2+ def count_seq ():
3+ # 초기값
4+ n = '2'
5+ while True :
6+ # 첫번째 함수 호출 시 int(n)을 리턴
7+ yield int (n )
8+ next_value = ''
9+
10+ # 두번째 호출부터 아래 코드 실행
11+ while len (n )> 0 :
12+ # 값의 처음값을 first에 저장, count 0으로 초기화
13+ first = n [0 ]
14+ count = 0
15+
16+ # 같은 값이 반복되면 카운트 증가
17+ # 값을 다음 자릿수로 이동(리스트 슬라이싱)
18+ while len (n )> 0 and n [0 ]== first :
19+ count += 1
20+ n = n [1 :]
21+
22+ # 변환된 값을 연결(문자열에서 + 은 문자열 연결)
23+ next_value += '{}{}' .format (count ,first )
24+
25+ n = next_value
26+
27+
28+ if __name__ == '__main__' :
29+ # 제너레이터 사용을 위해 함수 변수 선언
30+ gen = count_seq ()
31+
32+ # 10번 반복
33+ for i in range (10 ):
34+ # 제너레이터 호출은 next()으로 한다
35+ print (next (gen ))
36+
37+ """
38+ [실행 결과]
39+ 2
40+ 12
41+ 1112
42+ 3112
43+ 132112
44+ 1113122112
45+ 311311222112
46+ 13211321322112
47+ 1113122113121113222112
48+ 31131122211311123113322112
49+ """
You can’t perform that action at this time.
0 commit comments