- Notifications
You must be signed in to change notification settings - Fork 845
/
Copy path1.py
19 lines (16 loc) · 865 Bytes
/
1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 순차 탐색 소스코드 구현
defsequential_search(n, target, array):
# 각 원소를 하나씩 확인하며
foriinrange(n):
# 현재의 원소가 찾고자 하는 원소와 동일한 경우
ifarray[i] ==target:
returni+1# 현재의 위치 반환 (인덱스는 0부터 시작하므로 1 더하기)
return-1# 원소를 찾지 못한 경우 -1 반환
print("생성할 원소 개수를 입력한 다음 한 칸 띄고 찾을 문자열을 입력하세요.")
input_data=input().split()
n=int(input_data[0]) # 원소의 개수
target=input_data[1] # 찾고자 하는 문자열
print("앞서 적은 원소 개수만큼 문자열을 입력하세요. 구분은 띄어쓰기 한 칸으로 합니다.")
array=input().split()
# 순차 탐색 수행 결과 출력
print(sequential_search(n, target, array))