트 내 다른 값들보다 크거나 같은지 비교한다. 이 알고리즘이 옳은 결과를 반환할지, 크기가
N
인 문제에서 미만 연산을 몇 번 수행할지 확인해보자.
코드
1-3
A
에서 가장 큰 값의 위치를 찾는 다른 접근법
def alternate(A):
for v in A:
v_is_largest = True
➊
for x in A:
if v < x:
v_is_largest = False
➋
break
if v_is_largest:
return v
➌
return None
➍
➊
A
를 순회할 때, 각 값
v
는 가장 큰 값이 될 수 있다.
➋
v
가 다른 값(
x
)보다 작으면 순회를 멈추고
v
는 가장 큰 값이 아닌 것으로 기록한다.
➌
v_is_largest
가
True
이면
v
가
A
에서 최댓값이므로
v
를 반환한다.
➍
A
가 빈 리스트이면
None
을 반환한다.
alternate
()
는
A
에서 모든
x
값보다 작지 않은
v
값을 찾는다. 구현에 중첩
for
루프를 사용
한다. 이번에는
x
에 대한 내부
for
루프가 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.
O’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
I wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
I’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
I'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.