Question: Scala Programming Question: Extract all indices matching a given sequence. Write a scala function findPatternIndicesInList that inputs two lists of Strings, lst and pattern. The

Scala Programming

Question: Extract all indices matching a given sequence.

Write a scala function findPatternIndicesInList that inputs two lists of Strings, lst and pattern. The goal is to find all the indices in lst such that the list pattern occurs in lst at these indices.

Example 1

lst is List("quick", "brown", "fox", "quick", "brown", "dog") and pattern is List("quick", "brown"). Code should return: List(0, 3). This is because starting at positions 0 (the very beginning) and 3 (the 4th element), we have occurrences of the pattern pattern: List("quick", "brown").

Ex. 2

lst is the list List("a", "b", "a", "c", "a", "a", "b") and pattern is List("a", "b"). Your code should return List(0, 5) since starting at indices 0, 5 in lst, we have an occurrence of the patterrn pattern.

Ex. 3

lst is the list List("a", "b", "a", "c", "a", "a", "b") and pattern is List("a"). Your code should return List(0, 2, 4, 5).

Ex. 4

lst is the list List("a", "b", "a", "c", "a", "a", "b") and pattern is List("a", "b", "c"). Your code should return the empty list since there is no occurrence of the patterrn List("a", "b", "c") in the original list.

Hint: Please use the function extractSubList you wrote for the previous problem and compare lists using the == operator.

In [ ]:

 
def findPatternIndicesInList(lst: List[String], pat: List[String]): List[Int] = {
 // YOUR CODE HERE
 ???
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!