Question

2) Write an algorithm for a function called list_search which takes 2 parameters: a sequence of...

2) Write an algorithm for a function called list_search which takes 2 parameters: a sequence of elements and a value to search for. The function should search the sequence for the value and return all indicies of instances of the value in the list.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Function list_search()

Problem: Find all the indices of a given value in the given sequence of elements.

Inputs:
1. A sequence of elements, S
2. Value to search, V

Output:
1. A list of indices of all the instance of the value in list

Step 1: Start
Step 2: Initialize a list, LI, to store the list of indices
Step 3: Initialize an integer, I = 0, for counting the index
Step 4: For each element x in S
           4.1:   if x is equal to V, append I into LI
           4.2:   Increment I by 1
Step 5: Return LI
Step 6: Stop

The algorithm translated to python code:
def list_search(S,V):
   LI = []
   I = 0
   for x in S:
       if(x == V):
           LI.append(I)
       I = I + 1
   return LI


#Testing the program
print(list_search([1,4,3,4,5,6],4))

Output:

[1,3]

Note:
Time Complexity of the program: O(n)
because to find the indices of the given value in the list, the value should be compared with all the elements in the given input. Hence, the for loop will run for n times, where n is the number of elements in the list.

Add a comment
Know the answer?
Add Answer to:
2) Write an algorithm for a function called list_search which takes 2 parameters: a sequence of...
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
  • write an algorithm function called balance() in javascript that takes in a string and returns a...

    write an algorithm function called balance() in javascript that takes in a string and returns a strinf with balanced parantheses only. the string should be able to contain only parantheses, numbers, and letters. include comments of each portion of your code balance(“()()”) should return “()()” balance(“())”) should return “()” balance(“a(b)c”) should return “a(b)c” balance(“a(b)c())”) should return “a(b)c()”

  • Lisp program count-of (symbol list): Write a function named count-of that takes two parameters, a symbol...

    Lisp program count-of (symbol list): Write a function named count-of that takes two parameters, a symbol and a list. Count the number of instances of x in the list. Do not count the number of x in any sub-lists within the list. Test: count-of 'a '(a 'a(a c) d c a). Result: 2 trim-to (symbol list): Write a function named trim-to that takes a symbol and list as parameters. Return a new list starting from the first occurrence of the...

  • using c, Write a function called p4. The function takes 2 parameters, both of which are...

    using c, Write a function called p4. The function takes 2 parameters, both of which are C strings. p4 should return “true” (not 0) if the 2ndparameter is a substring of the 1st parameter, or “false” (0) otherwise. Forexample: char topic[] = “computer”; char substr[] = “put”; printf(“%d\n”, p4(topic, substr); // prints 1 substr[0] = ‘P’; printf(“%d\n”, p4(topic, substr); // prints 0 – case sensitive char s1[] = “ababcd”; char s2[] = “abcd” char s3[] = “baba”; char s4[] =...

  • Write a function in the C++called summary that takes as its parameters an input and output...

    Write a function in the C++called summary that takes as its parameters an input and output file. The function should read two integers find the sum of even numbers, the sum of odd numbers, and the cumulative product between two values lower and upper. The function should return the sum of even, odd, ad cumulative product between the lower and upper values. Please write program in C++.

  • 1. Write a Lisp function called piece which takes a single argument x and implements the followin...

    1. Write a Lisp function called piece which takes a single argument x and implements the following piecewise linear function: piece(x) = x if 0 < x < 1 2. Write a Lisp function called intList which takes two integer arguments, a and b and returns the list of all integers from a to b (inclusive at both ends). For example, (intList 3 8) should return (345678) 1. Write a Lisp function called piece which takes a single argument x...

  • Code written in NASM Function called findLargest Write a function called findLargest that receives two parameters:...

    Code written in NASM Function called findLargest Write a function called findLargest that receives two parameters: an unsigned doubleword array and the length of the array. The function must return the value of the largest array member in eax. Preserve all registers (except eax) that are modified by the function. Write a test program in main that calls findLargest three times, each call using a different array with different lengths. Function called countHits Write a function called named countHits that...

  • Using python 3.6.0 Write a function, called cubic, which takes in two parameters, say x and...

    Using python 3.6.0 Write a function, called cubic, which takes in two parameters, say x and y and computes the following formula: x3 + x + y and returns the computed value. Sample output 1: Enter x: 2 Enter y: 1 The value of the function is : 11 Sample output 2: Enter x: 1 Enter y: 3 The value of the function is : 5

  • python program please Write a function called backwards which takes two parameters. The first is a...

    python program please Write a function called backwards which takes two parameters. The first is a file object that has already been opened in read mode called essayfile. The second is called filename, which contains the filename for the output file that will need to be properly opened and closed in your function. The function should read from already opened file object line by line. Each line in the file should have the words on the line reversed. For example,...

  • Python 1 Write a function called sum_odd that takes two parameters, then calculates and returns the...

    Python 1 Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers, if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second. 3 To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use...

  • Make a public class called ManyExamples and in the class... Write a function named isEven which...

    Make a public class called ManyExamples and in the class... Write a function named isEven which takes in a single int parameter and returns a boolean. This function should return true if the given int parameter is an even number and false if the parameter is odd. Write a function named close10 which takes two int parameters and returns an int. This function should return whichever parameter value is nearest to the value 10. It should return 0 in the...

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