ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

https://app.codility.com/programmers/lessons/5-prefix_sums/genomic_range_query/
DNS ์‹œํ€€์Šค๋Š” A, C, G, T๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๊ณ  ๊ฐ๊ฐ์˜ impact factor๋Š” 1, 2, 3, 4์ด๋‹ค. P, Q ๋ฆฌ์ŠคํŠธ๋Š” ๊ฐ๊ฐ ์‹œ์ž‘๊ณผ ๋์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ง„๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.
๋งŒ์•ฝ CAGCCTA๋ผ๋Š” DNS ์‹œํ€€์Šค๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ P[0] = 2, Q[0] = 4๋ผ๋ฉด DNS ์‹œํ€€์Šค ์ค‘ 2~4์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์ด ํƒ์ƒ‰ ๋ฒ”์œ„์ด๋‹ค. (CAGCCTA)
ํƒ์ƒ‰ ๋ฒ”์œ„ ์ค‘ ๊ฐ€์žฅ ์ž‘์€ impact factor๋ฅผ ๊ฐ€์ง€๋Š” ๊ฐ’์„ ์ฐพ์•„์„œ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋œ๋‹ค.

๋ฌธ์ œ ํ’€์ด

prefix sum์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ์ง€๋งŒ, ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋กœ๋„ ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ๋‹ค.

์ฝ”๋“œ

def solution(S, P, Q):
    length = len(P)
    answer = []

    for i in range(length):
        prefix = S[P[i]:Q[i] + 1]
        try:
            prefix.index('A')
            answer.append(1)
        except ValueError:
            try:
                prefix.index('C')
                answer.append(2)
            except ValueError:
                try:
                    prefix.index('G')
                    answer.append(3)
                except ValueError:
                    answer.append(4)
    return answer
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€