유형 : 구현
풀이 시간 : 10분
<나의 풀이>
def solution(s):
temp = s.split(" ")
answer = []
for word in temp:
word = list(word)
newwd = []
for i in range(len(word)):
if i % 2 == 0:
newwd.append(word[i].upper())
else:
newwd.append(word[i].lower())
answer.append(''.join(newwd))
answer.append(' ')
a = ''.join(answer)
return a[:len(a)-1]
.upper() , .lower()
같은 메소드를 사용하였다.
충격적인 풀이가 있어 적어둔다,
def toWeirdCase(s):
return " ".join(map(lambda x: "".join([a.lower() if i % 2 else a.upper() for i, a in enumerate(x)]), s.split(" ")))
https://www.daleseo.com/python-enumerate/
파이썬의 enumerate() 내장 함수로 for 루프 돌리기
Engineering Blog by Dale Seo
www.daleseo.com
enumerate는 iteration을 하는 메서드로, index와 value tuple을 만든다 .
>>> for entry in enumerate(['A', 'B', 'C']):
... print(entry)
...
(0, 'A')
(1, 'B')
(2, 'C')
'프로그래머스 > Lv. 1' 카테고리의 다른 글
28. 최소직사각형 ★ (0) | 2024.07.14 |
---|---|
27. 삼총사 (itertools - combinations) (0) | 2024.07.12 |
25. 3진법 뒤집기 ★ (10진법 -> 3진법, 3진법 -> 10진법) (0) | 2024.07.12 |
24. 예산 ★ (그리디;;) (0) | 2024.07.12 |
23. 행렬의 덧셈 ★ (numpy library의 사용) (0) | 2024.07.12 |