-1

I want to time this searching algorithm using something I found on the internet

L=[8,2,1,111,8,3,4,6,5,7,10,9,12,11]
v=10

def searchlinear(L, v):
          
    i= 0
    for value in L:
        if value == v:
            return i
        i+= 1
    return len(L)


from random import randint
from timeit import repeat

def run_algorithm(algorithm, array):
  
    setup_code = f"from __main__ import {algorithm}" \
        if algorithm != "sorted" else ""

    stmt = f"{algorithm}({array})"

    times = repeat(setup=setup_code, stmt=stmt, repeat=3, number=10)

    print(f"Algorithm: {algorithm}. Minimum execution time: {min(times)}")

ARRAY_LENGTH = len(L)
array = [randint(0, 1000) for i in range(ARRAY_LENGTH)]

run_algorithm(algorithm="searchlinear", array=array)            

Somehow, it shows up this error:

TypeError: searchlinear() missing 1 required positional argument: 'v'

I feel like I have to remove the v argument in my function, but I need that for it to work. Any tips?

rohitt
  • 1,124
  • 6
  • 18
  • Aside from passing the wrong parameters, you're timing the list construction. You should put that in the setup. – user2357112 Dec 25 '20 at 12:42
  • Okay, so `array = [randint(0, 1000) for i in range(ARRAY_LENGTH)]` is the array that you want to be searched, when the test runs. **What value** do you want to search for? *How is the code intended to explain that*? – Karl Knechtel Oct 07 '22 at 20:25

1 Answers1

2

The searchlinear function expects 2 arguments. But, in the following line, you only passed the array. You have to pass the second argument too:

stmt = f"{algorithm}({array})"

So, change it to something like:

someValue = 7  # any number you want to search
stmt = f"{algorithm}({array}, {someValue})"
vighnesh153
  • 4,354
  • 2
  • 13
  • 27