KT
kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
Author: LukArToDo
Created: 16.feb 2018
Bugs: unknown
Description: The code finds sum of all positive elements in array and counts negative elements in array
*/
fun main(LukArToDo: Array<String>){
val test=arrayOf(arrayOf(1,-5,7,-3, 4),arrayOf(5,3,2,4),arrayOf(-4,-85,-4))
for(arr in test)
print("Sum of positive elements in ${arr.toList()} = ${sumOfPositive (arr)} \nNumber of negative elements in ${arr.toList()} = ${countNegative (arr)} \n\n")
}
fun sumOfPositive(arr:Array<Int>):Int= arr.filter{it>0}.fold(0) {x,y-> x + y}
fun countNegative(arr:Array<Int>):Int= arr.filter{it<0}.map{it}.size
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run