13.2 Composite Operators and Check Solution
13.2.1 Composite Operators
Now, we are ready to automate the three-step composite operators process. Let’s say we consider the number sequence in the second row of nums_perm
: 5, 1, 5, 5, with the ninth row of op_mat
: 1, 2, 3. We know the composite operation goes as below along with the corresponding steps printed.
<- cal(nums_perm[2, 1], nums_perm[2, 2], op_mat[9, 1])
re_1 print_op(nums_perm[2, 1], nums_perm[2, 2], re_1, op_mat[9, 1])
#> [1] "5 + 1 = 6"
<- cal(re_1, nums_perm[2, 3], op_mat[9, 2])
re_2 print_op(re_1, nums_perm[2, 3], re_2, op_mat[9, 2])
#> [1] "6 - 5 = 1"
<- cal(re_2, nums_perm[2, 4], op_mat[9, 3])
re_3 print_op(re_2, nums_perm[2, 4], re_3, op_mat[9, 3])
#> [1] "1 * 5 = 5"
To make our code more compact, we can write a loop over the three operators.
<- rep(0, n_nums)
sol 1] <- nums_perm[2, 1]
sol[for(step in 1:(n_nums-1)){
+ 1] <- cal(sol[step], nums_perm[2, step+1], op_mat[9, step])
sol[step cat(print_op(sol[step], nums_perm[2, step+1], sol[step + 1], op_mat[9, step]), "\n")
}#> 5 + 1 = 6
#> 6 - 5 = 1
#> 1 * 5 = 5
To prepare this step as a standalone function for computing all the steps, let’s write a function.
<- function(num, op, n_nums = length(num)){
num_op <- num
sol for(step in 1:(n_nums-1))
+ 1] <- cal(sol[step], num[step+1], op[step])
sol[step return(sol)
}<- num_op(nums_perm[2, ], op_mat[9, ])
sol sol
13.2.2 Check if Solution is Found
Once we finish the composite operations, we can check whether our final result is equal to the target value, 24.
<- 24
goal if(abs(sol[n_nums] - goal) < 1e-5){
cat("Found a solution!")
else {
} cat("This is not a solution.")
}#> This is not a solution.
If it does, we can print the solution. Let’s write another function to print all the intermediate steps.
<- function(nums, sol, op){
print_sol <- round(sol, 3) #Only keep 3 digits to make it better looking.
sol <- length(sol)
n_nums for(step in 1:(n_nums-1)){
cat(print_op(sol[step], nums[step+1], sol[step + 1], op[step]), "\n")
}
}print_sol(nums_perm[2, ], sol, op_mat[9, ])
13.2.3 Looping Over All Number Sequences and All Operator Sequences
Now, we know how to compute the final value given a number sequence and a operator sequence. Let’s write double loop over the number sequences and the operator sequences. Note that we only want to print the steps when we have found a solution.
<- 0
n_sols for(i in 1:nrow(nums_perm))
for(j in 1:nrow(op_mat)){
<- nums_perm[i, ]
num_cur <- op_mat[j, ]
op_cur <- num_op(num_cur, op_cur)
sol if(abs(sol[n_nums] - goal) < 1e-5){
<- n_sols + 1
n_sols cat("Solution #", n_sols, "\n")
print_sol(num_cur, sol, op_cur)
}
}#> Solution # 1
#> 1 / 5 = 0.2
#> 5 - 0.2 = 4.8
#> 4.8 * 5 = 24
if(n_sols == 0){
cat("Solution not exist!")
}
From the result, we can see that there is one solution to this difficult problem.