Advertisement
Week045

Lab #2

Sep 28th, 2023
1,132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.82 KB | None | 0 0
  1. import _root_.scala
  2.  
  3. import _root_.scala.annotation.tailrec
  4.  
  5. object Lab2 {
  6.  
  7.   //Tasks1
  8.   private def sumFunc1(list: List[Int], lenght: Int): Int = {
  9.     if(lenght <= 0) 0
  10.     else list.head + sumFunc1(list.tail, lenght - 1)
  11.   }
  12.  
  13.   private def sumFunc2(indexList: List[Int], numList: List[Int]): Int = {
  14.     if(indexList.isEmpty){
  15.       println("indexList is empty")
  16.       sys.exit(-1)
  17.     }
  18.     if(numList.isEmpty){
  19.       println("numList is empty")
  20.       sys.exit(-2)
  21.     }
  22.  
  23.     for(i <- indexList.indices){
  24.  
  25.     }
  26.     9
  27.   }
  28.  
  29.   private def func3(numList: List[Int]): Int = {
  30.     if (numList.isEmpty) {
  31.       println("numList is empty")
  32.       sys.exit(-3)
  33.     }
  34.     var minIndex: Int = 0
  35.     for(i <- 0 until numList.length)
  36.       if(numList(minIndex) > numList(i))
  37.         minIndex = i
  38.  
  39.      minIndex
  40.   }
  41.  
  42.   @tailrec
  43.   private def func4(numList: List[Int], number: Int): Boolean = {
  44.     if(numList.isEmpty) false
  45.     else if(numList.head > number) true
  46.     else func4(numList.tail, number)
  47.   }
  48.  
  49.   @tailrec
  50.   private def func5(numList: List[Int], number: Int, cnt: Int): Int = {
  51.     var count: Int = cnt
  52.     if(numList.isEmpty) count
  53.     else if(numList.head < number){
  54.       count += 1
  55.       func5(numList.tail, number, count)
  56.     }
  57.     else func5(numList.tail, number, count)
  58.   }
  59.  
  60.  
  61.  
  62.  
  63.   def main(args: Array[String]): Unit = {
  64.     val myList1: List[Int] = List(14, 42, 35, 64, 35, 65, 22, 31, 100, 4, 5, 37)
  65.     val myList2: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
  66.     val myList3: List[Int] = List(1, 2, 3, 4)
  67.  
  68.     //println(sumFunc1(myList2, if(myList2.length >= 5) 5 else myList2.length))
  69.     //println(func3(myList1))
  70.     println(func4(myList1, 100))
  71.     println(func5(myList2, 5, 0))
  72.  
  73.     //println(sumFunc2(myList2 take 5))
  74.     //println(sumFunc3(myList2))
  75.  
  76.   }
  77.  
  78.  
  79.  
  80.  
  81.  
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement