|
| 1 | +MDPyramid |
| 2 | +================ |
| 3 | + |
| 4 | +MDPyramid Doc |
| 5 | +------------- |
| 6 | + |
| 7 | +Simple MDPyramid script in R. Delivers 2 possible routes with maximum sums. |
| 8 | + |
| 9 | +The task to find the maximum sum of the numbers per the given rules below: |
| 10 | + |
| 11 | +1. You will start from the top and move downwards to an adjacent number as in below. |
| 12 | +2. You are only allowed to walk downwards and diagonally. |
| 13 | +3. You should walk over the numbers as evens and odds subsequently. Suppose that you are on an even number the next number you walk must be odd, or if you are stepping over an odd number the next number must be even. In other words, the final path would be like Odd -> even -> odd -> even … |
| 14 | +4. You must reach to the bottom of the pyramid. |
| 15 | + |
| 16 | +The goal is to find the maximum sum if you walk the path. |
| 17 | + |
| 18 | +- This is v02 |
| 19 | + |
| 20 | +Code |
| 21 | +---- |
| 22 | + |
| 23 | +R code in the document as follows: |
| 24 | + |
| 25 | +``` r |
| 26 | +pacman::p_load(tidyverse, data.table, stringr, magrittr,glue) |
| 27 | + |
| 28 | +file <- "https://raw.githubusercontent.com/vinciuna/MDPyramid/master/binaryTree_test.txt" |
| 29 | +dt <- readLines(con = file) |
| 30 | +dt |
| 31 | +``` |
| 32 | + |
| 33 | + ## [1] "215" |
| 34 | + ## [2] "192 124" |
| 35 | + ## [3] "117 269 442" |
| 36 | + ## [4] "218 836 347 235" |
| 37 | + ## [5] "320 805 522 417 345" |
| 38 | + ## [6] "229 601 728 835 133 124" |
| 39 | + ## [7] "248 202 277 433 207 263 257" |
| 40 | + ## [8] "359 464 504 528 516 716 871 182" |
| 41 | + ## [9] "461 441 426 656 863 560 380 171 923" |
| 42 | + ## [10] "381 348 573 533 448 632 387 176 975 449" |
| 43 | + ## [11] "223 711 445 645 245 543 931 532 937 541 444" |
| 44 | + ## [12] "330 131 333 928 376 733 017 778 839 168 197 197" |
| 45 | + ## [13] "131 171 522 137 217 224 291 413 528 520 227 229 928" |
| 46 | + ## [14] "223 626 034 683 839 052 627 310 713 999 629 817 410 121" |
| 47 | + ## [15] "924 622 911 233 325 139 721 218 253 223 107 233 230 124 233" |
| 48 | + |
| 49 | +``` r |
| 50 | +max <- dt %>% map_int(.,~str_count(.,"\\s+")) %>% max() |
| 51 | +dt %<>% map_chr(., ~str_c(., str_c(rep(" 0",max-str_count(.,"\\s")),collapse = ""),sep="")) |
| 52 | +dt <- str_split(dt,"\\s") |
| 53 | +dt <- pmap_dfc(dt,rbind) %>% mutate_all(.,as.integer) |
| 54 | +``` |
| 55 | + |
| 56 | +Functions: |
| 57 | + |
| 58 | +``` r |
| 59 | +dt_clean <- function(x,p) { |
| 60 | + if (p==1){ |
| 61 | + x[!(seq(x)%%2 == x%%2)] <- 0} else |
| 62 | + {x[(seq(x)%%2 == x%%2)] <- 0} |
| 63 | + x[x==0] <- -Inf |
| 64 | + x |
| 65 | +} |
| 66 | +#binary tree elements grouping |
| 67 | +split2 <- function(v, seg.length, seg.over) { |
| 68 | + if (length(v)< seg.length){return(v) } |
| 69 | + else{ |
| 70 | + starts = seq(1, length(v)-1, by=seg.length-seg.over) |
| 71 | + ends = starts + seg.length - 1 |
| 72 | + ends[ends > length(v)] = length(v) |
| 73 | + lapply(1:length(starts), function(i) v[starts[i]:ends[i]]) |
| 74 | + } |
| 75 | +} |
| 76 | +#summing vectors |
| 77 | +fadd <- function(v1,v2){ |
| 78 | + sums=v1+v2 |
| 79 | + max=max(sums) |
| 80 | + maxid=which(sums==max) |
| 81 | + adds=list(v1[v1!=-Inf], v2[v2!=-Inf]) |
| 82 | + if (max!=-Inf){ |
| 83 | + list(max=max, |
| 84 | + maxid=maxid, |
| 85 | + sums=sums[sums!=-Inf], |
| 86 | + adds=adds) |
| 87 | + } else return(-Inf) |
| 88 | +} |
| 89 | +#as.integer customized |
| 90 | +as.integer0 <- function(x) |
| 91 | +{ |
| 92 | + x <- as.integer(x) |
| 93 | + if (length(x)==0 ) x=-Inf |
| 94 | + else x=x |
| 95 | + x |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +Data setup: \* cleaning table with impossible |
| 100 | + |
| 101 | +``` r |
| 102 | +#data setup |
| 103 | +par <- dt[[1,1]]%%2 |
| 104 | +dt %<>% mutate_all(~dt_clean(.,par)) |
| 105 | +dt |
| 106 | +``` |
| 107 | + |
| 108 | + ## # A tibble: 15 x 15 |
| 109 | + ## V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 |
| 110 | + ## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> |
| 111 | + ## 1 215 -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf |
| 112 | + ## 2 192 124 -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf |
| 113 | + ## 3 117 269 -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf |
| 114 | + ## 4 218 836 -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf |
| 115 | + ## 5 -Inf 805 -Inf 417 345 -Inf -Inf -Inf -Inf -Inf -Inf -Inf |
| 116 | + ## 6 -Inf -Inf 728 -Inf -Inf 124 -Inf -Inf -Inf -Inf -Inf -Inf |
| 117 | + ## 7 -Inf -Inf 277 433 207 263 257 -Inf -Inf -Inf -Inf -Inf |
| 118 | + ## 8 -Inf 464 504 528 516 716 -Inf 182 -Inf -Inf -Inf -Inf |
| 119 | + ## 9 461 441 -Inf -Inf 863 -Inf -Inf 171 923 -Inf -Inf -Inf |
| 120 | + ## 10 -Inf 348 -Inf -Inf 448 632 -Inf 176 -Inf -Inf -Inf -Inf |
| 121 | + ## 11 223 711 445 645 245 543 931 -Inf 937 541 -Inf -Inf |
| 122 | + ## 12 330 -Inf -Inf 928 376 -Inf -Inf 778 -Inf 168 -Inf -Inf |
| 123 | + ## 13 131 171 -Inf 137 217 -Inf 291 413 -Inf -Inf 227 229 |
| 124 | + ## 14 -Inf 626 34 -Inf -Inf 52 -Inf 310 -Inf -Inf -Inf -Inf |
| 125 | + ## 15 -Inf -Inf 911 233 325 139 721 -Inf 253 223 107 233 |
| 126 | + ## # ... with 3 more variables: V13 <dbl>, V14 <dbl>, V15 <dbl> |
| 127 | + |
| 128 | +``` r |
| 129 | +n <- nrow(dt) |
| 130 | +x2 <- dt[n,] %>% as.numeric(.) |
| 131 | +lc <- seq(n) |
| 132 | +path <- as.list(lc);p_<- NULL |
| 133 | +``` |
| 134 | + |
| 135 | +Looping over nodes: |
| 136 | + |
| 137 | +``` r |
| 138 | +#loop over nodes |
| 139 | +for (i in (n-1):1 ) { |
| 140 | + x2 %<>% split2(.,2,1) |
| 141 | + lc %<>% split2(.,2,1) |
| 142 | + x1 <- dt[i,] %>% as.numeric(.) |
| 143 | + path[[i]] <- as.list(x=1:length(x2)) |
| 144 | + for (j in 1:length(x2) ){ |
| 145 | + add_ <- fadd(x1[j],x2[[j]]) |
| 146 | + if (is.list(add_)) { |
| 147 | + lc[[j]] <- j+add_$maxid-1 |
| 148 | + path[[i]][[j]] <- list(set_=j, |
| 149 | + nodes_= c(x2[[j]][[add_$maxid]],x1[j]), |
| 150 | + sum_=add_$max ) |
| 151 | + x2[[j]] <- add_$max |
| 152 | + } else { |
| 153 | + lc[[j]] <- -Inf |
| 154 | + x2[[j]] <- -Inf |
| 155 | + } |
| 156 | + } |
| 157 | + x2 %<>% unlist() |
| 158 | +} |
| 159 | +``` |
| 160 | + |
| 161 | +Rezults: |
| 162 | +======== |
| 163 | + |
| 164 | +``` r |
| 165 | +cat("sum=",path[[1]][[1]]$sum_,"\n") |
| 166 | +``` |
| 167 | + |
| 168 | + ## sum= 8186 |
| 169 | + |
| 170 | +``` r |
| 171 | +sum.l <- path[[1]][[1]]$nodes_[1] |
| 172 | +p_[1] <- path[[1]][[1]]$nodes_[2] |
| 173 | +for (i in 2:(n-1) ) { |
| 174 | + sums <- map(path[[i]],"sum_") %>% map(as.integer0) %>% unlist() |
| 175 | + j <- which(sums==sum.l) |
| 176 | + p_[i] <- map(path[[i]],"nodes_") %>% .[[j]] %>% .[2] |
| 177 | + #cat(p_,"+") |
| 178 | + sum.l <- path[[i]][[j]]$nodes_[1] |
| 179 | +} |
| 180 | +p_[n] <- map(path[[i]],'nodes_') %>% .[[j]] %>% .[1] |
| 181 | +glue("path: {paste0(p_,collapse = ' + ')}") |
| 182 | +``` |
| 183 | + |
| 184 | + ## path: 215 + 192 + 269 + 836 + 805 + 728 + 433 + 528 + 863 + 632 + 931 + 778 + 413 + 310 + 253 |
0 commit comments