알고리즘/코드워
[python]Simple Pig Latin
(ㅇㅅㅎ)
2020. 3. 13. 22:21
728x90
반응형
https://www.codewars.com/kata/520b9d2ad5c005041100000f/train/python
Codewars: Train your coding skills
Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential.
www.codewars.com
문자열에서 단어의 앞 알파벳을 단어의 뒤로 옮긴 뒤 'ay'를 덧 붙여서 출력하는 문제이다.
예) 'Pig' -> 'igPay'
더보기

내 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# My Code
def pig_it(text):
answer = []
for i in text.split():
if i.isalpha():
answer.append(i[1:] + i[0] + 'ay')
else:
answer.append(i)
return ' '.join(answer)
# 한줄로 표현하면 아래와 같다.
# return ' '.join([i[1:] + i[0] + 'ay' if i.isalpha() else i for i in text.split()])
if __name__=='__main__':
answer = pig_it('Pig latin is cool')
print(answer)
answer = pig_it('Quis custodiet ipsos custodes ?')
print(answer)
|
알파벳이 아닌 '!', '?', ',', '.' 등을 고려해야 했기에 isalpha()를 사용하여 나누어서 answer에 저장하도록 만들었습니다.
생각한 알고리즘을 코드로 작성한 다음 한 줄로 나타낼 수 있도록 코드를 제작해 보았습니다.

반응형