Algorithm๐Ÿงถ | ์‹œ์ € ์•”ํ˜ธ(findํ•จ์ˆ˜, ASCII Code)

์ „์— ํ’€์—ˆ๋˜ ๋ฌธ์ œ์ง€๋งŒ ์ •๊ทœํ‘œํ˜„์‹์„ ๋ณต์Šตํ•œ๋‹ค๋Š” ์ƒ๊ฐ์œผ๋กœ ๋‹ค์‹œ ํ’€์–ด๋ณด์•˜๋‹ค

https://programmers.co.kr/learn/courses/30/lessons/12926

๋ฌธ์ œ ์„ค๋ช…
์–ด๋–ค ๋ฌธ์žฅ์˜ ๊ฐ ์•ŒํŒŒ๋ฒณ์„ ์ผ์ •ํ•œ ๊ฑฐ๋ฆฌ๋งŒํผ ๋ฐ€์–ด์„œ ๋‹ค๋ฅธ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ๋ฐ”๊พธ๋Š” ์•”ํ˜ธํ™” ๋ฐฉ์‹์„ ์‹œ์ € ์•”ํ˜ธ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด "AB"๋Š” 1๋งŒํผ ๋ฐ€๋ฉด "BC"๊ฐ€ ๋˜๊ณ , 3๋งŒํผ ๋ฐ€๋ฉด "DE"๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. "z"๋Š” 1๋งŒํผ ๋ฐ€๋ฉด "a"๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด s์™€ ๊ฑฐ๋ฆฌ n์„ ์ž…๋ ฅ๋ฐ›์•„ s๋ฅผ n๋งŒํผ ๋ฏผ ์•”ํ˜ธ๋ฌธ์„ ๋งŒ๋“œ๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด ๋ณด์„ธ์š”.

์ œํ•œ ์กฐ๊ฑด
๊ณต๋ฐฑ์€ ์•„๋ฌด๋ฆฌ ๋ฐ€์–ด๋„ ๊ณต๋ฐฑ์ž…๋‹ˆ๋‹ค.
s๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž, ๋Œ€๋ฌธ์ž, ๊ณต๋ฐฑ์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
s์˜ ๊ธธ์ด๋Š” 8000์ดํ•˜์ž…๋‹ˆ๋‹ค.
n์€ 1 ์ด์ƒ, 25์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ

snresult
"AB"1"BC"
"z"1"a"
"a B z"4"e F d"

์ฒซ ๋ฌธ์ œํ’€์ด

def solution(s, n):
    Alpa = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    alpa = "abcdefghijklmnopqrstuvwxyz"
    answer = ""
    for i in s:
        if i == " ":
            answer += i
        elif i != " ":
            for j in range(len(Alpa)):
                if i == Alpa[j]:
                    if j+1 >= len(Alpa):
                        answer += Alpa[n-1]
                    elif j+n < len(Alpa):
                        answer += Alpa[j+n]
                elif i == alpa[j]:
                    if j+1 >= len(Alpa):
                        answer += alpa[n-1]
                    elif j+n < len(Alpa):
                        answer += alpa[j+n]
    return answer

3๊ฐœ์˜ ํ…Œ์ŠคํŠธ ํ†ต๊ณผ
์ฑ„์ ์€ ์‹คํŒจ๐Ÿ™„

๐Ÿ‘์„ฑ๊ณต

def solution(s, n):
    Alpa = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    alpa = "abcdefghijklmnopqrstuvwxyz"
    answer = ""
    for i in s:
        if i in Alpa:
            number = Alpa.find(i)+n
            answer += Alpa[number%26]
        elif i in alpa:
            number = alpa.find(i)+n
            answer += alpa[number%26]
        elif i == " ":
            answer += i
    return answer

๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด

๐Ÿ‘‡find ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ•œ๋‹ค

def solution(s, n):
    Alpa = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    alpa = "abcdefghijklmnopqrstuvwxyz"
    answer = ""
    for i in s:
        if i in Alpa:
            number = Alpa.find(i)+n
            answer += Alpa[number%26]
        elif i in alpa:
            number = alpa.find(i)+n
            answer += alpa[number%26]
        elif i == " ":
            answer += i
    return answer

๐Ÿ‘‡ASCII code๋ฅผ ์ด์šฉ

def caesar(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)

ASCII code
chr() : ์•„์Šคํ‚ค ์ฝ”๋“œ์—์„œ ํ•ด๋‹น๋˜๋Š” ๋ฌธ์ž์—ด์„ ๊ฐ€์ ธ์˜จ๋‹ค
ord() : ์•„์Šคํ‚ค ์ฝ”๋“œ๊ฐ’์„ ๋Œ๋ ค์ค€๋‹ค

https://ko.wikipedia.org/wiki/ASCII

ASCII code 10์ง„๋ฒ•

์ฝ”๋“œ ๊ฐ’ํ•ด๋‹น ๋ฌธ์ž์—ด
48~57[0-9]
65~90[A-Z]
97~122[a-z]

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ