MIPS | Malaysian Informatics And Programming Society


You are given a coordinates of a triangle’s corners, (x1,y1), (x2,y2) and (x3,y3). Find the coordinates of the centroid of the triangle (xc,yc), given that xc=(x1+x2+x3)/3 and yc=(y1+y2+y3)/3.

Input Format:

x1 y1
x2 y2
x3 y3

Output Format:

xc yc

Sample Input:

0 0
1 1
2 5

Sample Output:

1 2


This is a rather straightforward problem - we just have to apply the formula given in the statement. The main aim of this problem is to teach you how to read space-separated inputs and print space-separated outputs.

Handling Space-Separated Output

Notice something about the input and output format in this problem that is different from FizzBuzz and Representative - each line of input and output has 2 values instead of 1. In a programming contest, if the output is space-separated instead of line-separated, printing the output line-separated will result in a wrong answer.

Take note that

2 5

is different from


Suppose we have the coordinates of the centroid. The following program will result in a wrong answer.

Even though the values are correct, the format is wrong. To print more than one value on a line, we can use print(v1,v2,...). This will automatically separate each value with a space. Hence, the following will print the output in the format the problem specified.


Let’s try writing some code below that reads the coordinates of just one point x y using int(input()).

Our “Solution: https://repl.it/BKME/3

What happens if you input 0 0 into the program above? The following error occurs:

ValueError: invalid literal for int() with base 10: '0 0'

The error occurs because input() reads the input line by line. For each line of input, input() returns a string. So, when we keyed in 0 0, the program tried to type-cast the string “0 0” as an integer - which does not work. For the same reason, the following code will return an error when we key in two numbers in the same line.

Some problems in the MCC will have input and output formatted like Centroid, as it may be more natural to format the input and output as space-separated values rather than line separated values. Thus, we need a way of dealing with space-separated input.

Handling Space-Separated Input

We begin with the following code:

The above stores the x and y coordinates in a string. Keying in 2 5 results in the string “2 5”. We know that we cannot typecast this string into integers directly. However, we do know that we can typecast the string “2” into the integer 2 and the string “5” into the integer 5. So, if we can somehow split the string into the individual numbers, we can do the typecasting. Thankfully, we can split a string using the split() method. split() splits a string into a list of strings which are separated by white-space in the original string (i.e. if the string is a sentence, split() converts it into a list of the words in the sentence).

You can also use other values as separators (perhaps commas), but for our purposes we don’t have to do that.So now we have a slightly improved version of what we started with:

We now have a list of strings. We just have to apply the int() type-constructor to each string in the list, and then we are done.

Our Solution: https://repl.it/BKME/11

List Unpacking

Our current version of the code for reading the coordinates for the point is as follows.

However, the intermediate variable xy seems a bit unnecessary, as we ultimately want to work with x and y, and we’d like to avoid the hassle of indexing a list of strings. We can make our code neater by “unpacking” input().split() directly.

This works for any list, as long as the number of variables on the left hand side is equal to the number of elements in the list.We can now put all of the above together to read the input for the coordinates of a point.

Our Solution: https://repl.it/BKME/41

Note: Right now we are reading in some space-separated numbers which we’d like to assign to separate variables. If reading in a list of space-separated numbers, then it may be more natural to store the numbers in a list. How would you read the input in that scenario?The next section on alternative methods is optional. You can skip over to the last section to solve Centroid now.

Alternative Methods (Optional)

We can use the map() function or list comprehensions to read space-separated input. These are advanced concepts which are not required for the MCC, but you can read up on them if you’d like.


List Comprehension

Note: Instead, of using list comprehensions, you can also use generators in a similar fashion.


Now, you can solve Centroid.

Our Solution: https://repl.it/BKHZ/3