Goal:
Generate all possible sums and subtractions for kids of the form
10 - 4 = ?
4 + 3 = ?
Constraints:
- Only numbers between 0-10
- Result should be >= 0
Solution:
generate_sums <- function(maxresult=10) { sums <- c() for (i in 0:(maxresult-1)) { for (j in 0:(maxresult-i)) { sums <- c(sums, paste0(i, ' + ', j, ' = ?')) } } sample(sums[!duplicated(sums)]) } generate_subtractions <- function(maxresult=10) { substractions <- c() for (i in 0:maxresult) { for (j in i:maxresult) { substractions <- c(substractions, paste0(j, ' - ', i, ' = ?')) } } sample(substractions[!duplicated(substractions)]) }
I still believe that a more elegant approach should be possible but at least it works.
No comments:
Post a Comment