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
'10 어 Ga zi (이모저모고모) > 코딩테스트' 카테고리의 다른 글
[프로그래머스] Python - Lv.0 주사위 게임 2 (0) | 2024.04.22 |
---|---|
[프로그래머스] Python - Lv.0 등차수열의 특정한 항만 더하기 (0) | 2024.04.22 |
[프로그래머스] Python - Lv.0 flag에 따라 다른 값 반환하기 (0) | 2024.04.18 |
[프로그래머스] Python - Lv.0 조건 문자열 (0) | 2024.04.17 |
[프로그래머스] Python - Lv.0 홀짝에 따라 다른 값 반환하기 (0) | 2024.04.17 |