https://www.hackerrank.com/challenges/the-love-letter-mystery/problem?isFullScreen=true
The Love-Letter Mystery | HackerRank
Find the minimum number of operations required to convert a given string into a palindrome under certain conditions
www.hackerrank.com
이 문제의 핵심은
1. 알파벳 리스트 생성 - ascii_lowercase
2. index()사용
이다.
from string import ascii_lowercase
def theLoveLetterMystery(s):
alpha_list = list(ascii_lowercase)
answer = 0
if len(s) % 2 == 0: #스트링이 짝수개일때
for i in range(int(len(s)/2)):
if s[i] != s[len(s)-i-1]:
answer += abs(alpha_list.index(s[i]) - alpha_list.index(s[len(s)-i-1]))
else:
pass
else: #스트링이 홀수개일때
for i in range(int(len(s)/2)):
if s[i] != s[len(s)-i-1]:
answer += abs(alpha_list.index(s[i]) - alpha_list.index(s[len(s)-i-1]))
else:
pass
return answer
이 문제 같은 경우는 string이여서 find()를 사용했어도 무관하다. 하지만 index()의 경우 범용성이 더 좋아서 필자는 대부분 index()를 활용한다.
다음은 참조 링크이다.
find와 index의 차이
https://ooyoung.tistory.com/78
파이썬 find( ), index( ) 비교 / 인덱스, 위치를 찾는 함수(Python)
파이썬 find( ), index( ) - 순서 - 1.find / index 공통적인 내용 2.find / index 차이점 1. find( ), index( ) 공통적인 내용 1-1) 두 함수의 동일한 사용방법 '변수. find(찾을 문자)' / '변수. index(찾을 문..
ooyoung.tistory.com
string - ascii_lowercase
'파이썬 코딩테스트 > 해커랭크' 카테고리의 다른 글
| HackerRank / Sherlock and the Valid String / Python (0) | 2021.12.28 |
|---|---|
| HackerRank / String Construction / Python (0) | 2021.12.27 |
| HackerRank / Missing Number / Python (0) | 2021.12.26 |
| HackerRank / Smart Number / Python (0) | 2021.12.26 |
| HackerRank / Sherlock and Array / Python (0) | 2021.12.24 |