관리 메뉴

Silver Library (Archived)

Linear & binary search with Python 본문

F2. Problem & Solving/Solving

Linear & binary search with Python

Ayin Kim 2021. 11. 15. 22:28
반응형

Goal: To understand how to implement the concept in a real project.

 

Python

pos = -1

def search(list, n):

	l = 0
    u = len(list) -1
    
    while l <= u:
    	mid = (l+u) // 2
        
        if list[mid] == n:
        	globals()['pos'] = mid
            return True
        else:
        	if list[mid] < n:
            	l = mid;
            else:
            	u = mid;
                
    return False
                
list = [4, 7, 8, 12, 43, 46, 99, 102, 802, 702]
n = 802

if search(list, n):
	print("Found at ", pos+l)
else:
	print("Not Found")

'F2. Problem & Solving > Solving' 카테고리의 다른 글

10869, 2588 사칙연산 - Python [특이사항]  (0) 2021.12.26
Algorithm - change of plan (language)  (0) 2021.11.16
For in, For of 용도  (0) 2021.09.22
JS - wrapper object  (0) 2021.07.07
JS - switch  (0) 2021.07.06