Question

Create a function that takes a list and an integer v, and returns True if v...

Create a function that takes a list and an integer v, and returns True if v is in the list (False otherwise). The function should be efficient and stop searching as soon as possible. •The main program must generate a very large list with random elements, call the function to search a value and display the result.
Add in the function a variable Nsteps to count the number of steps used by the algorithm (number of times the loop is executed) and display a message with this information
In python
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import random
def findInt(L,num):
    Nsteps=0
    for j in range(len(L)):
        Nsteps=Nsteps+1
        if L[j]==num:
            print('Number found. The loop ran',Nsteps,'times')
            return True
    print('Number not found. The loop ran', Nsteps, 'times')
    return False


def main():
    L = random.sample(range(1, 1000), 800)
    print(findInt(L, 765))

if __name__ == '__main__':
    main()

import random def findInt (L, num): Nsteps-0 for j in range (len (L)): Nsteps Nsteps+ print (Number found. The loop ran,Nsteps,times) return True print (Number not found. The loop ran, Nsteps, times return False def main): L= random . sample (range (1, print (findInt (L, 765)) 1000), 800) name- main : main () main0 findlnt >X C:\Users VaibhavlAppData\Local\Programs\Python\Python37\python.exe C:/Users/V Number found. The loop ran 177 times True

Add a comment
Know the answer?
Add Answer to:
Create a function that takes a list and an integer v, and returns True if v...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT