프로그래머스. 시저 암호 파이썬 풀이

minan·2021년 6월 22일
0

프로그래머스

목록 보기
25/92

프로그래머스. 연습문제. Level 1. 시저 암호 파이썬 풀이

문제링크 https://programmers.co.kr/learn/courses/30/lessons/12926

isupper(), islower()이 있더라 그걸 쓰면 더 깔끔할 것 같다

def solution(s, n):
    answer = ''
    
    for c in s:
        # 공백일경우
        if c == " ":  
            answer += " "
        # 문자가 대문자고 밀었을 때 Z를 넘는 경우
        elif ord(c) <= 90 and ord(c) >= 65 and ord(c) + n > 90:
            answer += chr(64 + (ord(c) + n) % 90)
        # 문자가 소문자고 밀었을 때 z를 넘는 경우
        elif ord(c) <= 122 and ord(c) >= 97 and ord(c) + n > 122:
            answer += chr(96 + (ord(c) + n) % 122)
        # 아스키 코드로 변환해서 +n 만큼 밀고 다시 문자로 변환
        else: 
            answer += chr(ord(c) + n)
            
    return answer
profile
https://github.com/minhaaan

0개의 댓글