BA1D - Find All Occurrences of a Pattern in a String
*문제 링크 : https://rosalind.info/problems/ba1d/
네 번째 문제입니다. 이번 문제는 주어진 Sequence에 대해서, 특정 패턴이 확인될 때 인덱스를 반환하면 되는 문제입니다. 아래 예시가 있습니다.
ATAT
GATATATGCATATACTT
1 3 9
GATATATGCATATACTT, GATATATGCATATACTT, GATATATGCATATACTT 3번 등장하네요. 0-based index니 1 3 9를 반환하면 됩니다. ba1a랑 비슷하게 풀면 됩니다.
import pandas as pd
file = pd.read_table("../../Downloads/rosalind_ba1d.txt", header= None)
일단 불러오는 것 부터 시작합니다. 파일 경로는 알맞게 바꿔주시고요.
string = file[0][0]
sequence = file[0][1]
함수는 ba1a랑 큰 차이 없습니다. 함수의 input으로 들어갈 sequence와 string을 file 변수에서 찾아 명시해줍니다.
def MatchPattern(Text, Pattern) :
match_index = []
for i in range(len(Text)-len(Pattern)):
if Text[i : i+len(Pattern)] == Pattern :
match_index.append(i)
return match_index
혹시 자세한 설명이 필요하면 아래 링크를 참조 바랍니다. 바뀐건 빈 리스트를 쓰고, 마지막에 append로 인덱스를 추가해준다는 점 하나입니다.
[Rosalind] BA1A - Compute the Number of Times a Pattern Appears in a Text
BA1A - Compute the Number of Times a Pattern Appears in a Text * 문제 링크 : https://rosalind.info/problems/ba1a/ 첫 문제라서 그런가 슈도코드로 힌트를 주고 시작합니다. 가볍게 살펴볼까요. PatternC..
circuit.tistory.com
tmp = MatchPattern(sequence, string)
tmp
함수를 돌리고 그냥 저장은 pandas를 좀 써서 빠르게 처리했습니다.
pd.DataFrame(tmp).to_csv("../../Downloads/rosalind_ba1d_answer.txt", index=False)
맨 위 0은 지워야한다. 지우고 업로드 하면 해결 완료! 그럼 다음 문제로 넘어가 봅시다.
'Python study > Rosalind' 카테고리의 다른 글
[Rosalind] BA1C - Find the Reverse Complement of a String (0) | 2022.08.03 |
---|---|
[Rosalind] BA1B - Find the Most Frequent Words in a String (0) | 2022.08.02 |
[Rosalind] BA1A - Compute the Number of Times a Pattern Appears in a Text (0) | 2022.08.02 |