Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

:(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: Input List("Hello", "World", "My", "Cat", "Is", "Grumpy", "Today") Output List( (0, "Hello"), (1, "World"), (2, "My"), (3,"Cat"), (4, "Is"), (5, "Grumpy"), (6,"Today") ) You Are

(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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

=+b) Obtain a forecast for the week of May 28, 2007.

Answered: 1 week ago