Question: (I need help codding this problem; an explanation would be appreciated. In Python please, Thank you.) Given a list of pairs of integers, for example
(I need help codding this problem; an explanation would be appreciated. In Python please, Thank you.)
Given a list of pairs of integers, for example
~~~ List( (1, 5), (2, 7), (15, 14), (18, 19), (14, 28), (0,0), (35, 24) ) ~~~
Output a list consisting of just those pairs $(n_1, n_2)$ in the original list wherein $ | n_1 - n_2 | \leq 1 $. Ensure that the order of the elements in the output list is the same as that in the input list.
For the example above, the expected output is:
~~~ List( (15,14), (18,19), (0,0) ) ~~~
__Note__
- Your function must be called `outputConsecutivePairs` with just one argument that must be a list of pairs of integers. It must return a list of pairs of integers. - You can use for-loops and the following operators for concatenating elements to list. - `::` or cons appends an element to the front of a list. - `:+` appends an element to the back of a list. - `++` appends two lists together. - You can use list API functions: `reverse` and `length` - You should not use any other list API functions including `filter`, `map`, `foldLeft`, `foldRight` and so on. - Plenty of time to learn them properly later on. - Do not try to convert your list to an array or vector so that you can mutate it. - If you are unable to solve the problem without violating the restrictions or unsure, ask us privately on piazza or ask a TA during office hours. - There are no restrictions on the use of vars.
__Hints__
- In Scala, pairs of integers have the type `(Int, Int)` - A list containing pairs of integers has the type : `List[ (Int, Int) ]`. - Here is how one iterates over the elements of a list in Scala ~~~ for (elt <- list) { // do stuff with elt } ~~~ - Appending an element to the end of a list (see below). ~~~ result_list = result_list :+ elt // result_list must be declared as a var ~~~ - Appending an element to the front of a list : ~~~ result_list = elt:: result_list // using cons ~~~ - Appending an element to the end of a list ~~~ result_list = result_list ++ List(elt) // use list concatenation ~~~ - Warning: The append at end of a list `:+` (or other operations like list concat `++`) operation takes $O(n)$ time, where $n$ is the size of the list. We will often try to avoid it. But OK to use it for this particular part of problem 1.
// YOUR CODE HERE ???
//BEGIN TEST
val lst1 = List((1,1), (-1, 1), (1, -1)) val out1 = outputConsecutivePairs(lst1) testWithMessage( out1, List( (1,1)), "test1")
val lst2 = List((1, 2), (2, 1), (1,0), (0,1), (1,1)) val out2 = outputConsecutivePairs(lst2) testWithMessage( out2, lst2, "test2")
val lst3 = List((1,5), (5,1)) val out3 = outputConsecutivePairs(lst3) testWithMessage(out3, Nil, "test3")
val lst4 = List((2,5), (1,2), (-5, -4), (4,1)) val out4 = outputConsecutivePairs(lst4) testWithMessage(out4, List((1,2), (-5,-4)), "test4")
passed(5) //END TEST
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
