[Algorithm] Lv1. 둘만의 암호(programmers)

Intro

프로그래머스 Lv1. 문제인 둘만의 암호 문제에 대해 풀이하고자 합니다.

문제

img

입출력 예시

img

🥸 나의 접근 방법(오답)

  1. 알파벳 전체를 담은 문자열 alpha 변수를 선언한다
  2. skip 리스트를 별도로 만든다.
  3. skip 리스트에 s + index를 했을 때 alpha변수에 있을 경우 +1을 해준다.

🤩 다른 접근 방법(정답)

  1. 알파벳 전체를 담은 문자열 alpha 변수를 선언한다.
  2. replace를 사용하여 skip할 문자를 제거한다.
  3. 나머지 연산자를 사용하여 “skip에 있는 알파벳은 제외하고 건너뜁니다.” 의 규칙을 준수하여 풀이함.

❌오답 코드

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
def solution(s, skip, index):
    # skip은 제외할 인덱스번호, 
    answer = []
    alpha = "abcdefghijklmnopqrstuvwxyz"
    # print(len(alpha))
    skip_alpha = []
    # 스킵해야 할 인덱스 리스트 제작.
    for i in skip:
        skip_index = alpha.index(i)
        skip_alpha.append(skip_index)
    
    print("skip_alpha:", skip_alpha)

    # s의 요소들을 하나씩 꺼내서 인덱스를 계산하여 alpha에서 값을 가져올 것임.
    for i in s:
        s_index = alpha.index(i)
        s_index += index
        # s_index 범위 내에 skip_alpha가 있으면 +1을 해준다.
        for j in range(s_index-index, s_index):
            if j in skip_alpha:
                s_index += 1
        
        if s_index >= len(alpha):
            s_index -= len(alpha)
        
        
        answer.append(alpha[s_index])
    
        
    return print("".join(answer))

⭕️정답 코드(replace 사용)

1
2
3
4
5
6
7
8
9
10
11
12
13
def solution(s, skip, index):
    alpha = "abcdefghijklmnopqrstuvwxyz"
    answer = ""

    for i in skip:
        if i in alpha:
            alpha = alpha.replace(i, "")

    for i in s:
        if i in alpha:
            s_index = (alpha.index(i) + index) % len(alpha)
            answer += alpha[s_index]
    return answer

회고

  1. replace를 사용하여 스킵할 리스트 변수를 만들지 않아도 해결가능했다.
  2. 나머지 연산자를 사용하여 z로 넘어갈 경우 a로 설정하였음.
    1
    2
    3
    4
    5
    6
    
     # 오답 코드
     if s_index >= len(alpha):
         s_index -= len(alpha)
    
     # 개선한 코드
     s_index = (alpha.index(i) + index) % len(alpha)
    

참조 사이트

둘만의 암호 풀이!