๊ฐ๋ฐ์ผ์ง
[ํ๋ก๊ทธ๋๋จธ์ค] level1. ๋๋ง์ ์ํธ ๋ณธ๋ฌธ
A. ๋ฌธ์ ์ค๋ช
๋ ๋ฌธ์์ด s์ skip, ๊ทธ๋ฆฌ๊ณ ์์ฐ์ index๊ฐ ์ฃผ์ด์ง ๋, ๋ค์ ๊ท์น์ ๋ฐ๋ผ ๋ฌธ์์ด์ ๋ง๋ค๋ ค ํฉ๋๋ค. ์ํธ์ ๊ท์น์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
๋ฌธ์์ด s์ ๊ฐ ์ํ๋ฒณ์ index๋งํผ ๋ค์ ์ํ๋ฒณ์ผ๋ก ๋ฐ๊ฟ์ค๋๋ค.
index๋งํผ์ ๋ค์ ์ํ๋ฒณ์ด z๋ฅผ ๋์ด๊ฐ ๊ฒฝ์ฐ ๋ค์ a๋ก ๋์๊ฐ๋๋ค.
skip์ ์๋ ์ํ๋ฒณ์ ์ ์ธํ๊ณ ๊ฑด๋๋๋๋ค.
์๋ฅผ ๋ค์ด 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 ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
B. ๋ต์
def solution(s, skip, index):
answer = []
alphabets = list("abcdefghijklmnopqrstuvwxyz")
for skip_ch in skip:
alphabets.remove(skip_ch)
for ch in s:
answer.append(alphabets[(alphabets.index(ch)+index)%len(alphabets)])
return ''.join(answer)
C. ๊ฐ์
s์ ๊ธธ์ด๊ฐ ์งง๊ณ , skip์ ๊ธธ์ด ๋ํ ์งง๊ธฐ ๋๋ฌธ์ ์ํ๋ฒณ ์ ์ฒด๋ฅผ listํ ํ์ฌ ์ฌ์ฉํ๋ค.
๊ทธ๋ฌ๋ removeํจ์์ indexํจ์ ๋ฑ ๋ฐฐ์ดํ์ ์๊ฐ์ด ์๋ชจ๋๋ค. ์๊ฐ๋ณต์ก๋๊ฐ ์ข์ง ์๊ณ , ํ์ฅ์ฑ์๋ ์ข์ง ์๋ค.
from string import ascii_lowercase
def solution(s, skip, index):
result = ''
a_to_z = set(ascii_lowercase)
a_to_z -= set(skip)
a_to_z = sorted(a_to_z)
l = len(a_to_z)
dic_alpha = {alpha:idx for idx, alpha in enumerate(a_to_z)}
for i in s:
result += a_to_z[(dic_alpha[i] + index) % l]
return result
์์ ๊ฐ์ด dict๋ฅผ ์ฌ์ฉํ์ฌ, ์ธ๋ฑ์ค๋ฅผ ๊ธฐ๋กํ๋ฉด ์๊ฐ๋ณต์ก๋๊ฐ ๊ฐ์ํ๋ค..!
*์ถ๊ฐ๋ก list์ ์ ์ฉํ ๋ฉ์๋
append() | Adds an element at the end of the list |
clear() | Removes all the elements from the list |
copy() | Returns a copy of the list |
count() | Returns the number of elements with the specified value |
extend() | Add the elements of a list (or any iterable), to the end of the current list |
index() | Returns the index of the first element with the specified value |
insert() | Adds an element at the specified position |
pop() | Removes the element at the specified position |
remove() | Removes the item with the specified value |
reverse() | Reverses the order of the list |
sort() | Sorts the list |
์ถ์ฒ:https://www.w3schools.com/python/python_lists_methods.asp
'Algorithm๐จ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ํด์ : level2. ์ ํ๋ฒํธ ๋ชฉ๋ก (1) | 2024.10.17 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ํด์ : level1. ํฐ์ผ๋ชฌ (2) | 2024.09.27 |
[ํ๋ก๊ทธ๋๋จธ์ค] level1. ํ๋ฒ๊ฑฐ ๋ง๋ค๊ธฐ (0) | 2024.09.25 |
[ํ๋ก๊ทธ๋๋จธ์ค] level1. ํคํจ๋ ๋๋ฅด๊ธฐ (0) | 2024.09.24 |
[ํ๋ก๊ทธ๋๋จธ์ค] level1. ๊ฐ์ธ์ ๋ณด ์์ง ์ ํจ๊ธฐ๊ฐ (0) | 2024.09.23 |