In [1]:
data <- read.table("data.txt") #Using dataframe to align data
str(data)
In [2]:
data$possible <- apply(data, 1, function(x) {t <- sort(x); (t[1]+t[2]) > t[3]}) #Creating new column with the possible value
For Q2 we recreate a new dataframe to work with rows like in Q1. To do so we use to original colum to create a new matrix
In [3]:
data2 <- data.frame(do.call(rbind, lapply(data[,1:3], matrix, ncol = 3, byrow=T)))
str(data2)
In [4]:
data2$possible <- apply(data2, 1, function(x) {t <- sort(x); (t[1]+t[2]) > t[3]})
In [5]:
cat("Q1 - The number of possible triangles by row is: ", sum(data$possible), '\n')
cat("Q2 - The number of possible triangles by column is: ", sum(data2$possible), '\n')