You are given two Pairs (Pair<Int,Int>) as input. Write a function named sum() that receives two integer Pairs and returns a new Pair with the sum of the two pairs. The sum is obtained as the sum of the respective components.
Computer scienceProgramming languagesKotlinObject-oriented programmingObject-oriented programming usage
Pair and Triple
Sum of two maps
Report a typo
Sample Input 1:
1 2
3 4Sample Output 1:
(4, 6)Sample Input 2:
-1 1
1 -1Sample Output 2:
(0, 0)Write a program in Kotlin
fun sum(p1: Pair<Int, Int>, p2: Pair<Int, Int>): Pair<Int, Int> {
// write your code here
}
/* Do not change code below */
fun main() {
val (a, b) = readLine()!!.split(' ').map { it.toInt() }
val (c, d) = readLine()!!.split(' ').map { it.toInt() }
val pairOne = Pair(a, b)
val pairTwo = Pair(c, d)
println(sum(pairOne, pairTwo))
}
___
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.