Daily Programmer 261 (Easy / R)
Challenge 261 easy mode from reddit’s daily programmer. Verifying a 3x3 magic square.
Description
A 3x3 magic square is a 3x3 grid of the numbers 1-9 such that each row, column, and major diagonal adds up to 15. Here’s an example:
8 1 6
3 5 7
4 9 2
The major diagonals in this example are 8 + 5 + 2 and 6 + 5 + 4. (Magic squares have appeared here on r/dailyprogrammer before, in #65 [Difficult] in 2012.)
Write a function that, given a grid containing the numbers 1-9, determines whether it’s a magic square. Use whatever format you want for the grid, such as a 2-dimensional array, or a 1-dimensional array of length 9, or a function that takes 9 arguments. You do not need to parse the grid from the program’s input, but you can if you want to. You don’t need to check that each of the 9 numbers appears in the grid: assume this to be true.
Example inputs/outputs
[8, 1, 6, 3, 5, 7, 4, 9, 2] => true
[2, 7, 6, 9, 5, 1, 4, 3, 8] => true
[3, 5, 7, 8, 1, 6, 4, 9, 2] => false
[8, 1, 6, 7, 5, 3, 4, 9, 2] => false
Solution
verifyMagicSquare <- function(...) {
# Convert multiple numbers passed as arguments to numeric.
x <- matrix(as.numeric(list(...)), nrow = 3, ncol = 3, byrow = T)
sumrows <- apply(x, 1, sum)
sumcols <- apply(x, 2, sum)
sumdiag1 <- sum(diag(x))
sumdiag2 <- sum(diag(x[nrow(x):1,]))
if (length(unique(sumrows+sumcols)) != 1 | sumdiag1 !=15 | sumdiag2 != 15) {
return(FALSE)
} else {
return(TRUE)
}
}
verifyMagicSquare(8, 1, 6, 3, 5, 7, 4, 9, 2)
verifyMagicSquare(2, 7, 6, 9, 5, 1, 4, 3, 8)
verifyMagicSquare(3, 5, 7, 8, 1, 6, 4, 9, 2)
verifyMagicSquare(8, 1, 6, 7, 5, 3, 4, 9, 2)
## [1] TRUE
## [1] TRUE
## [1] FALSE
## [1] FALSE