A function is called Higher-Order Function if it contains other functions as a parameter or returns a function as an output. We can pass functions as arguments, we can return functions as well. All of this makes it very convenient to write generic code which can be reused in multiple places.
The function sort() or sorted() for instance, is a higher-order function. We can pass an argument called key to it, which will be used to determine how the sorting is done. We can have a list of tuples and would like to sort the list based on the sum of the elements of tuples:
The function do() has 3 arguments - the first number num1, the second number num2, and the operation that needs to be performed on those numbers. In the first call, we tell the function to use addition, in the second one to use subtraction, and in the final one multiplication.
Challenge
Given a list of n triplets of numbers, you are asked to sort those triplets by their sum. So, in the final list, if we look at the sum of the triplets, the result is a non-decreasing sequence.
The first line of the input contains a single integer n - the number of triplets. The next n lines contain 3 space-separated integers.
The program should print n lines - the resulting list of triplets. Each line should contain 3 space-separated numbers. If there are two triplets with the same sum, the first one appearing in the input should appear first in the output (sort and sorted functions keep that order - this is called stable sorting).