기억은 짧고 기록은 길다
[프로그래머스/Programmers] 시저 암호 - Python 본문
Link
코딩테스트 연습 - 시저 암호
어떤 문장의 각 알파벳을 일정한 거리만큼 밀어서 다른 알파벳으로 바꾸는 암호화 방식을 시저 암호라고 합니다. 예를 들어 "AB"는 1만큼 밀면 "BC"가 되고, 3만큼 밀면 "DE"가 됩니다. "z"는 1만큼 밀
programmers.co.kr
Solution
def solution(s, n):
answer = ''
for i in s:
if i == ' ':
answer += ' '
continue
r = ord(i)+n
if r > 122 and 97 <= ord(i) and ord(i) <= 122:
r -= 26
elif r > 90 and 65 <= ord(i) and ord(i) <= 90:
r -= 26
answer += chr(r)
return answer
🔑 key point: ord(), chr()
Other Solution
def solution(s, n):
s = list(s)
for i in range(len(s)):
if s[i].isupper():
s[i] = chr((ord(s[i]) - ord('A') + n) % 26 + ord('A'))
elif s[i].islower():
s[i] = chr((ord(s[i]) - ord('a') + n) % 26 + ord('a'))
return "".join(s)
'CodingTest > programmers' 카테고리의 다른 글
[프로그래머스/Programmers] 완주하지 못한 선수 - Python (0) | 2021.08.31 |
---|---|
[프로그래머스/Programmers] 약수의 합 - Python (0) | 2021.08.31 |
[프로그래머스/Programmers] 수박수박수박수박수박수 - Python (0) | 2021.08.31 |
[프로그래머스/Programmers] 소수 찾기_1 - Python (0) | 2021.08.31 |
[프로그래머스/Programmers] 서울에서 김서방 찾기 - Python (0) | 2021.08.27 |
Comments