728x90

시저 암호

 

문제 설명

어떤 문장의 각 알파벳을 일정한 거리만큼 밀어서 다른 알파벳으로 바꾸는 암호화 방식을 시저 암호라고 합니다. 예를 들어 "AB"는 1만큼 밀면 "BC"가 되고, 3만큼 밀면 "DE"가 됩니다. "z"는 1만큼 밀면 "a"가 됩니다. 문자열 s와 거리 n을 입력받아 s를 n만큼 민 암호문을 만드는 함수, solution을 완성해 보세요.

 

제한 조건

공백은 아무리 밀어도 공백입니다.

s는 알파벳 소문자, 대문자, 공백으로만 이루어져 있습니다.

s의 길이는 8000이하입니다.

n은 1 이상, 25이하인 자연수입니다.

입출력 예

s n result

"AB" 1 "BC"

"z" 1 "a"

"a B z" 4 "e F d" 

 

내 답

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def solution(s, n):
    answer = ''
    for i in range(len(s)):
        if s[i]==" ":
            answer += " "
            continue
        elif s[i]>='a' and s[i]<='z' and n>ord('z')-ord(s[i]):
            print(s[i])
            answer += chr(ord('a')+(n-(ord('z')-ord(s[i])))-1)
            continue
        elif s[i]>='A' and s[i]<='Z' and n>ord('Z')-ord(s[i]):
            print(s[i])
            answer += chr(ord('A')+(n-(ord('Z')-ord(s[i])))-1)
            continue
        answer += chr(ord(s[i])+n)
    
    return answer
cs

 

남의 답 1

 

1
2
3
4
5
6
7
8
9
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)
cs

 

 

남의 답 2

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def solution(s, n):
    lower_list = "abcdefghijklmnopqrstuvwxyz"
    upper_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
    result = []
 
    for i in s:
        if i is " ":
            result.append(" ")
        elif i.islower() is True:
            new_ = lower_list.find(i) + n
            result.append(lower_list[new_ % 26])
        else:
            new_ = upper_list.find(i) + n
            result.append(upper_list[new_ % 26])
    return "".join(result)
cs

 

 

남의 답 3

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import string
def solution(s, n):
    result = ""
    base = ""
    for c in s:
        if c in string.ascii_lowercase:
            base = string.ascii_lowercase
        elif c in string.ascii_uppercase:
            base = string.ascii_uppercase
        else:
            result += c
            continue
        a = base.index(c) + n
        result += base[a % len(base)]
    return result
cs

 

 

남의 답 4

 

1
2
3
4
5
6
7
8
9
10
11
def solution(s, n):
    result = ""
    for i in s:
        if i.isalpha():
            if i.islower():
                result += chr((ord(i) - ord("a"+ n) % 26 + ord("a"))
            else:
                result += chr((ord(i) - ord("A"+ n) % 26 + ord("A"))
        else:
            result += i
    return result
cs

 

 

남의 답 5

 

1
2
3
4
5
6
7
8
9
10
def solution(s, n):
    answer = ''
    for i in s:
        if i:
            if i >= 'A' and i <= 'Z':
                answer += chr((ord(i) - ord('A'+ n) % 26 + ord('A'))
            elif i >= 'a' and i <= 'z':
                answer += chr((ord(i) - ord('a'+ n) % 26 + ord('a'))
            else : answer += ' '
    return answer
cs

 

 

남의 답 6

 

1
2
def solution(s, n):
    return ''.join([chr(ord(i) + (not ord(i)==32)*((n%26-26*((90<(ord(i)+(n%26))*(ord(i)<91)) + (122<(ord(i)+(n%26)))))) for i in s])
cs

 

 

1157점 + 6점 -> 1163점

 

728x90
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기