Write in Scala for the following assignment: Define a function named countOccurs
ID: 3823947 • Letter: W
Question
Write in Scala for the following assignment:
Define a function named countOccurs that takes a List and an Int as parameters. The function should return the number of times the Int occurs in the List. For example: countOccurs List (6, 4, 2, 1, 0), 3) = 0 countOccurs(List (3, 3, 2, 3, 1, 4), 3) = 3 countOccurs(List(), 3) = 0 Test your code using the Scala compiler on stdlinux to check your work. Your solution MUST only use the elements we have discussed in class and the course notes, not other Scala constructs available on the web (you have enough to do this). Your solution MUST also use the notation and conventions we have discussed in class and must not use a while loop. As a hint check out slides 28-31 of the Scala slides where the List class is discussed. Consider trying to use either List's "foreach'' method to solve this one (using the sum example on slide 30 as a guide) OR List's match-case syntax (using the insert example on Slide 29 as a guide). If you want to do some exploration on your own, you are also allowed to use the List method "foldRight''. This is probably the "right'' way to solve this problem in Scala, but we will not be discussing it in class.Explanation / Answer
Please find my implementation.
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.