KEEP GOING

[python] 프로그래머스 12930번 : 이상한 문자 만들기 본문

code review/implementation

[python] 프로그래머스 12930번 : 이상한 문자 만들기

jmHan 2022. 3. 29. 12:05
반응형

https://programmers.co.kr/learn/courses/30/lessons/12930#

 

코딩테스트 연습 - 이상한 문자 만들기

문자열 s는 한 개 이상의 단어로 구성되어 있습니다. 각 단어는 하나 이상의 공백문자로 구분되어 있습니다. 각 단어의 짝수번째 알파벳은 대문자로, 홀수번째 알파벳은 소문자로 바꾼 문자열을

programmers.co.kr

 

 

문제 힌트 : 

 

입력값이 'hello '인 경우 (마지막에 공백 포함)

출력값 'HeLlO ' (공백 포함)

 

문제의 테스트케이스는 소문자 문자열으로 주어짐 ex "try hello world"

모든 문자열에 대해서는 홀수 짝수를 나누어 짝수일 때는 문자를 소문자 처리

 

 

 

코드 구현)

def solution(s):
    answer = []
    split_s = s.split(' ') # 문자열을 공백을 기준으로 분리
    for s in split_s:
        tmp = ''
        for idx, w in enumerate(s):
            if idx % 2 == 0: # 짝수인 경우
                tmp += w.upper()
            else: # 홀수인 경우
                tmp += w.lower()
        answer.append(tmp) 
    return ' '.join(answer)

 

split_s = s.split() 

ex) s = ['hello world ']

     split_s = s.split()

     print(split_s)

    >> ['hello', 'word']

 

split_s = s.split(' ') -> 마지막 공백에 대해 처리 가능 

ex) s = ['hello world ']

     split_s = s.split()

     print(split_s)

    >> ['hello', 'word', '']

반응형
Comments