Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import _root_.scala
- import _root_.scala.annotation.tailrec
- object Lab2 {
- //Tasks1
- private def sumFunc1(list: List[Int], lenght: Int): Int = {
- if(lenght <= 0) 0
- else list.head + sumFunc1(list.tail, lenght - 1)
- }
- private def sumFunc2(indexList: List[Int], numList: List[Int]): Int = {
- if(indexList.isEmpty){
- println("indexList is empty")
- sys.exit(-1)
- }
- if(numList.isEmpty){
- println("numList is empty")
- sys.exit(-2)
- }
- for(i <- indexList.indices){
- }
- 9
- }
- private def func3(numList: List[Int]): Int = {
- if (numList.isEmpty) {
- println("numList is empty")
- sys.exit(-3)
- }
- var minIndex: Int = 0
- for(i <- 0 until numList.length)
- if(numList(minIndex) > numList(i))
- minIndex = i
- minIndex
- }
- @tailrec
- private def func4(numList: List[Int], number: Int): Boolean = {
- if(numList.isEmpty) false
- else if(numList.head > number) true
- else func4(numList.tail, number)
- }
- @tailrec
- private def func5(numList: List[Int], number: Int, cnt: Int): Int = {
- var count: Int = cnt
- if(numList.isEmpty) count
- else if(numList.head < number){
- count += 1
- func5(numList.tail, number, count)
- }
- else func5(numList.tail, number, count)
- }
- def main(args: Array[String]): Unit = {
- val myList1: List[Int] = List(14, 42, 35, 64, 35, 65, 22, 31, 100, 4, 5, 37)
- val myList2: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
- val myList3: List[Int] = List(1, 2, 3, 4)
- //println(sumFunc1(myList2, if(myList2.length >= 5) 5 else myList2.length))
- //println(func3(myList1))
- println(func4(myList1, 100))
- println(func5(myList2, 5, 0))
- //println(sumFunc2(myList2 take 5))
- //println(sumFunc3(myList2))
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement