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

Write a Python function that will return the index of an element in a list.1- The...
Write a Python function that will return the index of an element in a list.1- The function will receive a list of at least 5 numbers as a single argument when the function is called. 2. The function will ask the user to input a number and will find and return the index of that number in the list.3. If the number is not an element of the list, the function returns the string "unknown."
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 Python program containing a function named scrabble_sort that will sort a list of strings...
Write a Python program containing a function named scrabble_sort that will sort a list of strings according to the length of the string, so that shortest strings appear at the front of the list. Words that have the same number of letters should be arranged in alphabetical order. Write your own logic for the sort function (you may want to start from some of the existing sorting code we studied). Do NOT use the built-in sort function provided by Python....
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...
[ RACKET] a) Write a recursive function (gen-list start end). This function will generate a list...
[ 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...
You are given a singly linked list. Write a function to find if the linked list...
You are given a singly linked list. Write a function to find if the linked list contains a cycle or not. A linked list may contain a cycle anywhere. A cycle means that some nodes are connected in the linked list. It doesn't necessarily mean that all nodes in the linked list have to be connected in a cycle starting and ending at the head. You may want to examine Floyd's Cycle Detection algorithm. /*This function returns true if given...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT