본문 바로가기
Engineering WIKI/Programmers

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

by wonos 2022. 4. 20.

 

 

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

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

programmers.co.kr

내 풀이

def solution(s):
    answer = ''
    index = 0
    
    for word in s:
        if word == " ":
            answer += " "
            index = 0
        elif index == 0 or index % 2 == 0:
            answer += word.upper()
            index += 1
        else:
            answer += word.lower()
            index += 1
        
    return answer

 

다른방법 1

def toWeirdCase(s):
    res = []
    for x in s.split(' '):
        word = ''
        for i in range(len(x)):
            c = x[i].upper() if i % 2 == 0 else x[i].lower()
            word = word + c
        res.append(word)
    return ' '.join(res)

# 아래는 테스트로 출력해 보기 위한 코드입니다.
print("결과 : {}".format(toWeirdCase("try hello world")));