Advertisement
drochun

Scala lab2

Sep 27th, 2023
1,235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.70 KB | None | 0 0
  1. object Main1 {
  2.   def isNegative(x: Int): Boolean = x < 0
  3.   def sumNegative(lst: List[Int]): Int = {
  4.     val negativeList = lst.filter(isNegative)
  5.     if(negativeList.isEmpty) 0
  6.     else {
  7.       val result = negativeList.foldLeft(0)((ac_c, x) => ac_c + x)
  8.       result
  9.     }
  10.   }
  11.   def findTriplicate(lst: List[Int]): Option[Int] = {
  12.   lst.find(x => lst.count(_ == x) == 3)
  13.   }
  14.   def main(args: Array[String]): Unit = {
  15.     val myList = List(1, -1, -3, 2, 4, 1, 1, 5, 7, 9)
  16.     println(myList.mkString(", "))
  17.     val sum = sumNegative(myList)
  18.     val tripple = findTriplicate(myList)
  19.     println(s"1.Sum of negative numbers: $sum")
  20.     println(s"5.Number that appears in list 3 times: $tripple")
  21.   }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement