#python #anaconda #jupyter-notebook #visual-code #html #css #javascript #http

제목Step_15_Search 실습 2번 질문2021-05-07 18:51
작성자

import time


def time_it(searchmethod, lst, value):

    t1 = time.perf_counter()

    searchmethod(lst, value)

    t2 = time.perf_counter()

    return (t2-t1) *1000


def linear_search_for(L, value):

    i = 0

    for i in range(len(L)):

        if L[i] == value:

            return i

    return -1


def linear_search_while(lst, value):

    i = 0

    while lst[i] != value and i != len(lst):

        i = i + 1

        

    if i == len(lst):

        return -1

    

    else: 

        return i

    

def sentinel_linear_search(L, value):

    L.append(value)

    i = 0

    while L[i] != value:

        i = i+1

    L.pop()

    

    if i == len(L):

        return -1

    else: 

        return i


def binary_search(L, value):

    i = 0

    j = len(L)-1

    m = int ((i+j) / 2)

    while i != j+1:

        if L[m] < value :

            i = m+1

        else: 

            j = m -1

    if 0 <= i < len(L) and L[i] == v: 

        return i

    else: 

        return -1

    

L = list(range(10000001))


time1 = time_it(linear_search_for, L, 10)

time2 = time_it(linear_search_for, L, 5000000)

time3 = time_it(linear_search_for, L, 10000000)


time4 = time_it(sentinel_linear_search, L, 10)

time5 = time_it(sentinel_linear_search, L, 5000000)

time6 = time_it(sentinel_linear_search, L, 10000000)


time7 = time_it(linear_search_while, L,10)

time8 = time_it(linear_search_while, L,5000000)

time9 = time_it(linear_search_while, L,10000000)


time10 = time_it(binary_search, L, 10)

time11 = time_it(binary_search, L, 5000000)

time12 = time_it(binary_search, L, 10000000)


newlist = []

a= ['method', 'linear_search_for', 'sentinel_linear_search', 'linear_search_while', 'binary_search']

b= ['10', time1, time4, time7, time10]

c= ['5000000', time2, time5, time8, time11]

d= ['10000000', time3, time6, time9, time12]

newlist.append(a)

newlist.append(b)

newlist.append(c)

newlist.append(d)


print(newlist)



우선 list가 잘 만들어지는지 구현해볼려고 하는데 오류도 뜨지 않고 아무것도 출력되지 않습니다. 

무엇이 문제인지 알려주시면 감사하겠습니다 ㅠ

댓글
이전step14 마지막문제 질문2021-05-09
다음step15 time_it 결과 e2021-05-07