Write in Scala for the following assignment: Define a function named remove that
ID: 3823949 • Letter: W
Question
Write in Scala for the following assignment:
Define a function named remove that takes an integer atom and a list as parameters (the list does not need to be sorted). The function should return a list with the integer removed from it. If the integer occurs multiple times in the list, all occurrences should be removed. For example: remove 3, List (1, 2, 3, 4, 5) 1, 2, 4, 5) remove 3, List(1, 2, 3, 3, 3, 4) 1, 2, 4) remove(3, List (3, 3, 3) = () remove 3, List (2, 4, 6, 8)) (2, 4, 6, 8) Test your code using the interpreter on stdlinux to check your work. As above, use only the elements we have discussed in class to write this code, and provide a solution done without using a while loop.Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
object Test {
def remove(x : Int, list : List[Int]) : List[Int] = {
val newList = list.filter(ele => ele != x)
newList
}
def main(args: Array[String]): Unit = {
println(remove(3, List(1,2,3,4,5,3,2)))
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.