Searching & max / min
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 이 페이지는 최신 브라우저(SharedArrayBuffer 지원)가 필요합니다. Chrome, Edge, Firefox 또는 Safari를 최신 버전으로 업데이트해 주세요.
English
Linear search
- A linear search looks at each item in turn until it finds the target.
- It is a named standard method in the syllabus.
- Use a loop and remember when you find a match.
한국어
선형 검색
- 선형 탐색은 타겟을 찾을 때까지 각 항목을 순서대로 확인합니다.
- 이는 교양 과정의 명명된 표준 방법입니다.
- 루프를 사용하고 일치하는 것을 찾았을 때 기억하십시오.
nums = [4, 9, 2, 7]
target = 9
found = False
for n in nums:
if n == target:
found = True
print(found) # True
Explore · 탐색하기
Linear search, step by step
A linear search checks each item in turn until it finds the target.
English
Finding the maximum
- Start by assuming the first item is the biggest.
- Then check the rest; if one is bigger, remember it instead.
한국어
최대값 찾기
- 첫 번째 항목이 가장 크다고 가정합니다.
- 나머지 항목을 확인합니다. 하나라도 더 크다면, 대신 그것을 기억합니다.
nums = [4, 9, 2, 7]
biggest = nums[0]
for n in nums:
if n > biggest:
biggest = n
print(biggest) # 9
English
Finding the minimum
- The same idea, but keep the smallest value seen so far.
한국어
최소값 찾기
- 같은 아이디어를 사용하되, 가장 작은 값을 유지합니다.
nums = [4, 9, 2, 7]
smallest = nums[0]
for n in nums:
if n < smallest:
smallest = n
print(smallest) # 2
English
In the exam: standard methods
- Linear search and finding maximum / minimum / average are named in section 7.
한국어
시험: 표준 방법
- 선형 탐색 및 최대/최소/평균 구하는 방법은 7절에서 다룹니다.
Found ← FALSE
FOR I ← 1 TO 4
IF Number[I] = Target
THEN
Found ← TRUE
ENDIF
NEXT I
English
Common mistakes
- Linear search checks each item in turn until it finds the target.
- For max or min, start from the first item, then compare the rest against it.
한국어
흔한 실수
- 선형 탐색은 각 항목을 순서대로 확인하여 목표 값을 찾습니다.
- 최대 또는 최소값을 찾을 때는 첫 번째 항목부터 시작하여 나머지 항목들과 비교합니다.
English
Now you try
- Loop through the list to search or compare.
- Press Check answer to test your code.
한국어
이제 직접 해보기
- 리스트를 반복하여 탐색하거나 비교합니다.
- Answer 확인 버튼을 눌러 코드를 테스트하세요.
Write contains(nums, target) that returns True if target is in the list, using a linear search loop (do not use the in keyword).
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
The list nums is set. Find the largest value with a loop and store it in biggest (do not use max()).
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Find the smallest value in nums with a loop and store it in smallest (do not use min()).
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.