Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object Main1 {
- def isNegative(x: Int): Boolean = x < 0
- def sumNegative(lst: List[Int]): Int = {
- val negativeList = lst.filter(isNegative)
- if(negativeList.isEmpty) 0
- else {
- val result = negativeList.foldLeft(0)((ac_c, x) => ac_c + x)
- result
- }
- }
- def findTriplicate(lst: List[Int]): Option[Int] = {
- lst.find(x => lst.count(_ == x) == 3)
- }
- def main(args: Array[String]): Unit = {
- val myList = List(1, -1, -3, 2, 4, 1, 1, 5, 7, 9)
- println(myList.mkString(", "))
- val sum = sumNegative(myList)
- val tripple = findTriplicate(myList)
- println(s"1.Sum of negative numbers: $sum")
- println(s"5.Number that appears in list 3 times: $tripple")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement