Question

In: Computer Science

PYTHON: Write a recursive function named linear_search that searches a list to find a given element....

PYTHON:

Write a recursive function named linear_search that searches a list to find a given element. If the element is in the list, the function returns the index of the first instance of the element, otherwise it returns -1000.

Sample Output

>> linear_search(72, [10, 32, 83, 2, 72, 100, 32])

4

>> linear_search(32, [10, 32, 83, 2, 72, 100, 32])

1

>> linear_search(0, [10, 32, 83, 2, 72, 100, 32])

-1000

>> linear_search('a', ['c', 'a', 'l', 'i', 'f', 'o', 'r', 'n', 'i', 'a'])

1

Use this template:

=============================================================================

def linear_search(ele, array):
"""searches the given list for the given element
INPUT:
* ele - the element
* array - the list
OUTPUT: the index of the first instance of the element in the list, or -1000 if it is not in the list.
"""

pass

Solutions

Expert Solution

def linear_search(ele, array):
    for i in range(len(array)):
      if array[i] == ele:
         return i
    return -1000

print(linear_search(72, [10, 32, 83, 2, 72, 100, 32]))
print(linear_search(32, [10, 32, 83, 2, 72, 100, 32]))
print(linear_search(0, [10, 32, 83, 2, 72, 100, 32]))
print(linear_search('a', ['c', 'a', 'l', 'i', 'f', 'o', 'r', 'n', 'i', 'a']))
4                                                                                                                            
1                                                                                                                            
-1000                                                                                                                        
1 


Related Solutions

I have the following question: Write a recursive function to find the Nth element from the...
I have the following question: Write a recursive function to find the Nth element from the top of a stack. For example, if N is 3 the function should return the third element in the stack. Use the following header: template <class Type> Type getNth( stack<Type> & s, int n) Although your function may push and pop values from the stack, it must eventually leave the stack in its original state. Your function may NOT use a help stack or...
Write a c++ member function that sequentially searches for a specific element in a doubly linked...
Write a c++ member function that sequentially searches for a specific element in a doubly linked list. return the position if found or -1 is the element cannot be found.
1.)recursive merge sort on a list.(Python) 2.)recursive bubble sort using a list without enumerate() function.(python) Show...
1.)recursive merge sort on a list.(Python) 2.)recursive bubble sort using a list without enumerate() function.(python) Show Base case, and recursive case.
Write a recursive and an iterative function to calculate the nth element in a Fibonacci sequence....
Write a recursive and an iterative function to calculate the nth element in a Fibonacci sequence. A Fibonacci sequence is defined as the element 1, followed by another 1, and each element thereafter is the sum of the previous two elements. For example, the first 9 elements of a Fibonacci sequence are: 1 2 3 5 8 13 21 34 This famous sequence was originally used to predict the growth of rabbit populations! Once you have each of the functions...
write a recursive algorithm to find the maximum element in an array of n elements and...
write a recursive algorithm to find the maximum element in an array of n elements and analyze its time efficiency. (I am using c++ programming language)
RACKET a) Write a recursive function (gen-list start end). This function will generate a list of...
RACKET a) Write a recursive function (gen-list start end). This function will generate a list of consecutive integers, from start to end. If start > end then an empty list is generated. For example: (gen-list 1 5) ---> (1 2 3 4 5) b) write a recursive function pair-sum? that takes an integer sequence as generated by the gen-list function in exercise 4 above. This function tests whether any two adjacent values in the given list sum to the given...
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and...
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and returns a new sequence that is twice the length of the parameter sequence but that contains the contents of the original in palindrome form. For example, if the sequence "super" is passed into the function, the function will return "superrepus".
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of...
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of integers. Assume that the List type has members: int List.length returns the length of the list. void List.push(T n) pushes an element n to the front of the list T List.pop() pops an element from the front of the list. List$$ List$$.concat(List$$ other) returns the concatenation of this list with other. Explain in plain English the reasoning behind your algorithm. Power Lists should be...
Write a recursive function named multiply that takes two positive integers as parameters and returns the...
Write a recursive function named multiply that takes two positive integers as parameters and returns the product of those two numbers (the result from multiplying them together). Your program should not use multiplication - it should find the result by using only addition. To get your thinking on the right track: 7 * 4 = 7 + (7 * 3) 7 * 3 = 7 + (7 * 2) 7 * 2 = 7 + (7 * 1) 7 *...
Given a list x = [2,3,5,6,7,8,9], write a python program that find those numbers which are...
Given a list x = [2,3,5,6,7,8,9], write a python program that find those numbers which are divisible by 3. Arrange these numbers in a list called “result”. Hint: similar to page 9 in the slides.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT