-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSizerCode.py
More file actions
73 lines (61 loc) · 1.74 KB
/
Copy pathSizerCode.py
File metadata and controls
73 lines (61 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 19. 시저 암호
def solution(s, n):
answer = ''
for char in s:
if char.isalpha():
if 65 <= ord(char) <= 90:
if ord(char) + n > 90:
answer += chr(ord(char) - 26 + n)
else:
answer += chr(ord(char) + n)
else:
if ord(char) + n > 122:
answer += chr(ord(char) - 26 + n)
else:
answer += chr(ord(char) + n)
else:
answer += ' '
return answer
s_1 = 'AB'
s_2 = 'z'
s_3 = 'a B z'
n_1 = 1
n_2 = 1
n_3 = 4
print(solution(s_1, n_1))
print(solution(s_2, n_2))
print(solution(s_3, n_3))
def solution_best(s, n):
answer = ''
str = list(s)
for i in range(len(str)):
if str[i].isupper():
str[i] = chr((ord(str[i]) - ord('A') + n) % 26 + ord('A'))
elif str[i].islower():
str[i] = chr((ord(str[i]) - ord('a') + n) % 26 + ord('a'))
answer = ''.join(str)
return answer
print(solution_best(s_1, n_1))
print(solution_best(s_2, n_2))
print(solution_best(s_3, n_3))
# 틀린 풀이
def solution_error(s, n):
answer = ''
# string을 ascii로 바꾼 리스트
str_ascii_lst = [ord(i) for i in s]
# print(str_ascii_lst)
for idx, cost in enumerate(str_ascii_lst):
if cost == 32:
str_ascii_lst[idx] -= 4
str_ascii_lst[idx] += n
# print(str_ascii_lst)
for i in str_ascii_lst:
idx = str_ascii_lst.index(i)
if 90 < i and i < 97:
rest = i - 90
str_ascii_lst[idx] = 64 + rest
elif 122 < i:
rest = i - 122
str_ascii_lst[idx] = 96 + rest
answer = ''.join(chr(i) for i in str_ascii_lst)
return answer