Question: Need help in Scala. Write a recursive procedure that returns true and only if the list has consecutive duplicates, i.e., a number occurs at least

 Need help in Scala. Write a recursive procedure that returns true

Need help in Scala.

Write a recursive procedure that returns true and only if the list has consecutive duplicates, i.e., a number occurs at least twice in succession. Note that the property is trivially "false" for lists of sizes 0 and 1. Example List(0, 1, 1, 2, 3, 4, 5, 2) has consecutive duplicates "1" followed by "1". List(0, 1, 0, 1, 0, 1, 0, 1) does not have consecutive duplicates (although it has duplicates). ]: def hasConsecutiveDupes (lst: NumList): Boolean = { // YOUR CODE HERE //??? ]: //BEGIN TEST val 11 = Cons(12, Cons (25, Cons (12, Nil))) assert(!hasConsecutiveDupes (11), "Test case 1: hasConsecutuveDupes (11) -- should return false") val 12 = Cons(14, Cons(14, Cons(13, 11 ))) assert (hasConsecutiveDupes (12), "Test case 2: hasConsecutiveDupes (12) -- should return true") val 13 = Cons(0, Cons(-2, Cons(-2, 11 ))) assert(hasConsecutiveDupes (13), "Test case 3: hasConsecutiveDupes (12) -- should return true") assert(!has ConsecutiveDupes (Nil), "Test case 4: hasConsecutiveDupes (Nil) -- should return false") val 15 - Cons(-1, Nil) assert(!hasConsecutiveDupes (15), "Test case 5: hasConsecutiveDupes (15) -- should return false") passed (7) //END TEST Write a recursive procedure that returns true and only if the list has consecutive duplicates, i.e., a number occurs at least twice in succession. Note that the property is trivially "false" for lists of sizes 0 and 1. Example List(0, 1, 1, 2, 3, 4, 5, 2) has consecutive duplicates "1" followed by "1". List(0, 1, 0, 1, 0, 1, 0, 1) does not have consecutive duplicates (although it has duplicates). ]: def hasConsecutiveDupes (lst: NumList): Boolean = { // YOUR CODE HERE //??? ]: //BEGIN TEST val 11 = Cons(12, Cons (25, Cons (12, Nil))) assert(!hasConsecutiveDupes (11), "Test case 1: hasConsecutuveDupes (11) -- should return false") val 12 = Cons(14, Cons(14, Cons(13, 11 ))) assert (hasConsecutiveDupes (12), "Test case 2: hasConsecutiveDupes (12) -- should return true") val 13 = Cons(0, Cons(-2, Cons(-2, 11 ))) assert(hasConsecutiveDupes (13), "Test case 3: hasConsecutiveDupes (12) -- should return true") assert(!has ConsecutiveDupes (Nil), "Test case 4: hasConsecutiveDupes (Nil) -- should return false") val 15 - Cons(-1, Nil) assert(!hasConsecutiveDupes (15), "Test case 5: hasConsecutiveDupes (15) -- should return false") passed (7) //END TEST

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!