Plotting Squares

The term sounds terrifying, but it doesn’t need to be: Data analysis is the process of using code to examine data and come up with some sort of conclusion. Obviously it helps to know the type of conclusion before you write the code. Only in science fiction does someone say, “What do you make all of those seemingly boring numbers, Mr. Computer?”

For a recent data analysis project, I had to write code that collected meteorological information and calculated averages. The project was basically a lot of math on a large quantity of numbers, which is something the computer is rather good at doing.

This month’s Exercise involves looking at a set of coordinates on a grid and determining whether or not those coordinates map out a square. The grid is illustrated in Figure 1. Rows are given numbers 1 through 7, columns are labeled A through H.

Figure 1. The data grid.

Figure 1. The data grid.

A sample data set might be: C1, C3, E1, E3

If you plot those points on the grid, you can visually see a square, but that’s because you’re a human and humans are great at noticing patterns. Computers? Not so much.

The code you write must calculate the distance between column coordinates and row coordinates as four corners on a square. All four sides must be equal, which is the definition of a square. (Assume that all rows and columns are evenly spaced in the grid.)

For this Exercise, your code must examine multiple data sets. Each set contains four pairs of coordinates. The code determines whether or not the data set defines a square.

Here are the data sets:

Data set 0: B2, B6, F2, E5
Data set 1: B1, B4, E1, E4
Data set 2: C2, C5, G2, G5
Data set 3: E6, E7, F6, F7

Assume that the coordinates are presented in this order: Upper left, lower left, upper right, lower right.

Here is a major hint: The issue with data analysis in C isn’t so much manipulating the data as it is presenting the data in a proper format. Some formats are easier to work with than others.

Obviously this puzzle has more than one solution. And you’ll need to be careful when coding the solution because some of the coordinates might fool less robust code into finding a square where none exists. So check everything!

Click here to view my solutions. As usual, please attempt this Exercise on your own before you peek at what I’ve done.

Leave a Reply