Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(I need help coding for problem B but posted part A because they use some of the same perimeters; an explanation would be appreciated. In

(I need help coding for problem B but posted part A because they use some of the same perimeters; an explanation would be appreciated. In Python, please, Thank you.)

Write a function `findAllOddElementsAndNext` wherein - Input a list of integers $[e_1, \ldots, e_n]$, - Return list of pairs of integers $[(e_{i}, e_{i+1}), (e_{j}, e_{j+1}), \ldots, (e_k, e_{k+1})]$ such that the first elements of the pair $e_i, e_j, \ldots, e_k$ are all odd elements and the second elements of the pair $e_{i+1}, e_{j+1}, \ldots, e_{k+1}$ are the subsequent elements in the list. - If an odd element happens to be the very last element, it should not appear in the output. - The order of elements appearing in the output list must be the same as that in the input list.

#### Example

~~~ Input: List(1, 5, 0, 2, 3, 7) Output: List( (1,5), (5,0), (3,7) )

Input: List(19) Output: List() //Empty list

Input: List(25, 36) Output: List( (25, 36) ) ~~~

You may use the previously implemented function `findAllOddIndices` as a subroutine.

**Restrictions** same as problem 1 but we will allow you to use the following operations if you need them. - To access the $i^{th}$ element in the list you are allowed to use `list(i)`. However, note that it takes $i$ steps to traverse a linked list to reach the $i^{th}$ element. Using this operation in a loop is guaranteed to give you $O(n^2)$ complexity. - The very first element of a list can be accessed by `list.head` - The tail of the list (from second to last element) can be accessed as `list.tail` - You can find the size of a list by `list.length`.

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

//BEGIN TESTS val lst1 = List(1, 5, 0, 2, 3, 7) val expected1 = List( (1,5), (5,0), (3,7) ) testWithMessage(findAllOddElementsAndNext(lst1), expected1, "Test 1")

val lst2 = List(17) val expected2 = List() testWithMessage(findAllOddElementsAndNext(lst2), expected2, "Test 2")

val lst3 = List(17, 14) val expected3 = List( (17, 14)) testWithMessage(findAllOddElementsAndNext(lst3), expected3, "Test 3")

val lst4 = List(7, 3, 5, 5, 3, 9) val expected4 = List((7,3), (3,5), (5,5), (5,3), (3,9)) testWithMessage(findAllOddElementsAndNext(lst4), expected4, "Test 4")

val lst5 = List(7, 0, 3, 2, 5, 2, 5, 4, 3, 6, 9) val expected5 = List((7,0), (3,2), (5,2), (5,4), (3,6)) testWithMessage(findAllOddElementsAndNext(lst5), expected5, "Test 5")

passed(13)

//END TESTS

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

More Books

Students also viewed these Databases questions

Question

Explain the various techniques of Management Development.

Answered: 1 week ago