Hi all, I’ve hit a wall while trying to implement a (somewhat) trivial problem:
I can only modify the RunDataStructure class, and can’t implement a different pop() method on LIFO() class itself.
abstract class DataStructures
case class FIFO() extends DataStructures
case class LIFO() extends DataStructures
class SelectDataStructure(val structure: DataStructures){}
class RunDataStructure[A](val sds: SelectDataStructure){
var elements: List[A] = Nil
//basic FIFO implementation - push() method is the same for LIFO too
def push(element: A): Boolean = {
elements = element :: elements
true
}
//need to override pop() so it's different for LIFO()
def pop(): Option[A] = {
if (elements == Nil) {
return None
}
val head = elements.reverse.head
elements = elements.reverse.tail.reverse
Some(head)
}
}
I have to run and pass these test cases (which I shouldn’t modify either)
"A priority collection" should "pop the smallest element first" in {
val sds= new SelectDataStructure(FIFO())
val fifo= new RunDataStructure[Int](sds)
fifo.push(1)
fifo.push(2)
fifo.push(3)
assert(fifo.pop() === Some(1))
assert(fifo.pop() === Some(2))
assert(fifo.pop() === Some(3))
}
"A LIFO collection" should "pop elements that were inserted first after elements that were inserted later" in {
val sds= new SelectDataStructure(LIFO())
val lifo= new RunDataStructure[Int](sds)
lifo.push(1)
lifo.push(2)
lifo.push(3)
assert(lifo.pop() === Some(3))
assert(lifo.pop() === Some(2))
assert(lifo.pop() === Some(1))
}
Is there a way to override pop() functions from the RunDataStructure class so they differ when they’re called on the tests?