# FUNCTIONS
is_even <- function(x) x%%2 == 0
is_hit <- function(x) is_even(x[1]) & !is_even(x[2]) | !is_even(x[1]) & is_even(x[2])
gen_data <- function(x) sample(seq(from = 1, to = x),replace = TRUE
mk_pairs <- function(x) split(x, ceiling(seq_along(x)/2))
prepare <- function(x) mk_pairs(gen_data(x))
exchange <- function(x) if(is_hit(x)) rev(x) else x
swap <- function(x) unlist(lapply(prepare(x), exchange), recursive = FALSE, use.names = FALSE)
# MAIN
set.seed(137)
swap(100)
#> [1] 34 59 8 38 79 39 96 93 35 65 30 48 13 43 14 22 4 6 89 70 48 15 13 32 14
#> [26] 95 35 97 55 35 86 35 47 16 20 15 71 96 13 14 69 64 51 99 70 91 38 87 54 65
#> [51] 38 47 8 44 25 96 22 9 69 91 58 30 21 55 89 45 73 50 3 18 46 96 36 31 44
#> [76] 60 78 55 15 89 1 89 27 94 59 43 44 35 40 83 43 40 81 14 46 37 43 10 7 59
set.seed(137)
unlist(prepare(100), recursive = FALSE, use.names = FALSE)
#> [1] 59 34 8 38 79 39 93 96 35 65 30 48 13 43 14 22 4 6 70 89 15 48 32 13 95
#> [26] 14 35 97 55 35 35 86 16 47 15 20 96 71 14 13 64 69 51 99 91 70 87 38 65 54
#> [51] 47 38 8 44 96 25 9 22 69 91 58 30 21 55 89 45 50 73 18 3 46 96 31 36 44
#> [76] 60 55 78 15 89 1 89 94 27 59 43 35 44 83 40 40 43 14 81 37 46 10 43 7 59
Created on 2020-10-17 by the reprex package (v0.3.0.9001)