10 어 Ga zi (이모저모고모)/코딩테스트
[프로그래머스] Python - Lv.0 코드 처리하기
디룡
2024. 4. 18. 08:43
https://school.programmers.co.kr/learn/courses/30/lessons/181932
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제
- mode의 초기값 : 0
- ret가 빈 문자열이면 "EMPTY" return
조건 | 결과 | |
mode = 0 | code[idx] = "1" | mode 바꾸기 |
code[idx] != "1" and idx가 짝수 | ret += code[idx] | |
mode = 1 | code[idx] = "1" | mode 바꾸기 |
code[idx] != "1" and idx가 홀수 | ret += code[idx] |
코드
def solution(code):
answer = ''
mode = False
for idx in range(len(code)) :
if mode == 0 :
if code[idx] == "1" :
mode = not mode
else :
if idx % 2 == 0 :
answer += code[idx]
else :
if code[idx] == "1" :
mode = not mode
else :
if idx % 2 == 1 :
answer += code[idx]
if not answer :
return "EMPTY"
return answer