-
[프로그래머스] 이상한 문자 만들기Engineering WIKI/Programmers 2022. 4. 20. 11:33
코딩테스트 연습 - 이상한 문자 만들기
문자열 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")));
'Engineering WIKI > Programmers' 카테고리의 다른 글
[프로그래머스] 정수 제곱근 판별 (0) 2022.04.24 [프로그래머스] 정수 내림차순으로 배치하기 (0) 2022.04.22 [프로그래머스] 자연수 뒤집어 배열로 만들기 (0) 2022.04.21 [프로그래머스] 자릿수 더하기 (0) 2022.04.21 [프로그래머스] 약수의 합 (0) 2022.04.19 [프로그래머스] 시저 암호 (0) 2022.04.19 [프로그래머스] 문자열을 정수로 바꾸기 (0) 2022.04.19 [프로그래머스] 소수 찾기 (0) 2022.04.19