Question
:(Need In Scala) 2C (7 Points): Convert A List Into An Indexed List. Write A Function To Convert A List Of Strings Into An Indexed
(need in scala)
2C (7 points): Convert a list into an indexed list.
Write a function to convert a list of strings into an indexed list of strings. Indices start at 0. As an example:
InputList("hello", "world", "my", "cat", "is", "grumpy", "today")
OutputList( (0, "hello"), (1, "world"), (2, "my"), (3,"cat"), (4, "is"), (5, "grumpy"), (6,"today") )
You are allowed to use just the basic list operatios such as cons of an element to a list (::) and concatenation of two lists (++, or ::: operators). List API functions reverse, map, filter, foldLeft and foldRight but not other list API functions. Do not usevar,loopsorrecursion.
In [ ]:
def makeIndexedList(lst:List[String]): List[(Int, String)] = {
??? // YOUR CODE HERE
}
In [ ]:
// BEGIN TEST
val t1 = makeIndexedList(List("hello"))
assert(t1 == List((0,"hello")), s"Test 1 failed - your code returned $t1")
val t2 = makeIndexedList(List("hello", "world"))
assert(t2 == List((0,"hello"), (1, "world")), s"Test 2 failed - your code returned $t2")
val t3 = makeIndexedList(Nil)
assert(t3 == Nil, s"Test 3 failed - your code returned $t3")
val t4 = makeIndexedList(List("a","b","c","d","e"))
assert(t4 == List((0,"a"), (1,"b"), (2,"c"), (3,"d"), (4,"e")), s"Test 4 failed - your code returned $t4")
passed(8)
// END TEST
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