Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 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 TESTStep 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