두 문자열 s와 skip, 그리고 자연수 index가 주어질 때, 다음 규칙에 따라 문자열을 만들려 합니다. 암호의 규칙은 다음과 같습니다.
예를 들어 s = "aukks", skip = "wbqd", index = 5일 때, a에서 5만큼 뒤에 있는 알파벳은 f지만 [b, c, d, e, f]에서 'b'와 'd'는 skip에 포함되므로 세지 않습니다.
따라서 'b', 'd'를 제외하고 'a'에서 5만큼 뒤에 있는 알파벳은 [c, e, f, g, h] 순서에 의해 'h'가 됩니다. 나머지 "ukks" 또한 위 규칙대로 바꾸면 "appy"가 되며 결과는 "happy"가 됩니다.
두 문자열 s와 skip, 그리고 자연수 index가 매개변수로 주어질 때 위 규칙대로 s를 변환한 결과를 return하도록 solution 함수를 완성해주세요.
s | skip | index | result |
---|---|---|---|
"aukks" | "wbqd" | 5 | "happy" |
본문 내용과 일치합니다.
def solution(s, skip, index):
answer = ""
alpha = "abcdefghijklmnopqrstuvwxyz"
for skip_word in skip:
if skip_word in alpha:
alpha = alpha.replace(skip_word, "")
for i in s:
change = alpha[(alpha.index(i) + index) % len(alpha)]
answer += change
return answer
def solution(s, skip, index):
atoz = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
for i in skip:
atoz.remove(i)
ans = ''
for i in s:
ans += atoz[(atoz.index(i)+index)%len(atoz)]
return ans