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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started