-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryTree_MDPyramid_.Rmd
172 lines (154 loc) · 4.79 KB
/
binaryTree_MDPyramid_.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
---
title: "MDPyramid"
output: github_document
---
```{r setup, include=FALSE, echo=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# MDPyramid Doc
Simple MDPyramid script in R.
Delivers possible route with maximum sums.
The task to find the maximum sum of the numbers per the given rules below:
1. You will start from the top and move downwards to an adjacent number as in below.
2. You are only allowed to walk downwards and diagonally.
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 …
4. You must reach to the bottom of the pyramid.
The goal is to find the maximum sum if you walk the path.
* This is v03
* added test with random numbers and random matrix sizes
## Code
R code in the document as follows:
```{r warning=FALSE}
pacman::p_load(tidyverse, data.table, stringr, magrittr,glue,gdata)
txt <- read.delim("https://raw.githubusercontent.com/vinciuna/MDPyramid/master/binaryTree_test.txt", stringsAsFactors = F, header = F)
n <- nrow(txt)
dt <- matrix(-Inf, nrow = n, ncol = n)
for (i in 1:n) {
dt[i,1:i] <- as.numeric(unlist(strsplit(txt[i,], " ")))
}
```
### Functions:
```{r functions}
#data cleansing
dt_clean <- function(x,p) {
if (p==1){
x[!(seq(x)%%2 == x%%2)] <- 0} else
{x[(seq(x)%%2 == x%%2)] <- 0}
x[x==0] <- -Inf
x
}
path.sum <- function(dat) {
#max_s_p <- NULL
#browser()
for (ii in nrow(dat):2) {
path[[ii-1]] <<- as.list(1:(ncol(dat)-1))
for (jj in 1:(ncol(dat)-1)) {
max_n <- max(dat[ii,jj:(jj + 1)])
#hich(dat[ii,jj:(jj + 1)]==max_)
max_s <- max_n + dat[ii-1, jj]
dat[ii-1,jj] <- max_s
#max_s_p <- c(max_s_p, dat[ii-1, jj])
path[[ii-1]][[jj]] <<- c(max_n,max_s)
}
dat[ii,] <- NA
}
return( max(dat, na.rm = TRUE))
}
```
### Data setup and call functions
```{r data setup}
#data setup
if (n==1) {
answer <- dt[1,1]
} else{
path <- as.list(1:(nrow(dt)-1))
par <- dt[[1,1]]%%2
dt <- as.data.frame(dt) %>% mutate_all(~dt_clean(.,par))
answer <- path.sum(dt)
}
```
### Rezults:
```{r rezults}
if (answer==-Inf) {
cat(" path not found...\n")
} else {
cat(" sum= ",path[[1]][[1]][[2]],"\n",sep="")
sum_ <- path[[1]][[1]][2]
n_ <- path[[1]][[1]][1]
id_ <- 1
if (length(path)>1){
cat( " path: ",sum_-n_,"+",sep="")
for (i in 2:(n-1) ) {
sum_ <- map(path[[i]],2) %>% unlist()
id_ <- which(sum_==n_)
if (length(id_)>1) {
id_ <- id_[1]
}
n_ <- map(path[[i]],1) %>% unlist() %>% .[id_]
cat( sum_[id_]-n_ , "+",sep="")
if (i==(n-1)) {
cat( n_,"\n")
}
}
} else {
cat( " path: ",sum_-n_,"+",n_,sep="")
}
}
```
## Testing pack:
```{r}
set.seed(123)
tests <- 200
output_file_csv <- "binominal_trees"
write(paste0("tests=",tests,"begin",sep=" "),file = output_file_csv,append=F)
for (t in seq(tests)) {
cat("# test=",t,":\n")
write(paste0("test=",t,":\n",sep=" "),file = output_file_csv,append=T)
n <- sample(1:50,1,replace = T)
cat(" n=",n,"\n")
write(paste0(" n=",n,"\n",sep=" "),file = output_file_csv,append=T)
dt <- matrix(-Inf, ncol = n, nrow = n)
if (n==1) {answer <- dt[1,1]}
else{
for (smpl in seq(n)) {
dt[smpl,1:smpl] <- c(sample(-99:999,smpl,replace = F))
}
path <- as.list(1:(nrow(dt)-1))
par <- dt[[1,1]]%%2
dt <- as.data.frame(dt) %>% mutate_all(~dt_clean(.,par))
write.fwf(dt,output_file_csv,append = T,sep="\t")
answer <- path.sum(dt)
}
if (answer==-Inf) {
cat(" path not found...\n")
write(" path not found...\n" ,file = output_file_csv,append=T)
} else {
cat(" sum= ",path[[1]][[1]][[2]],"\n",sep="")
write(paste0(" sum= ",path[[1]][[1]][[2]],"\n",sep=""),file = output_file_csv,append=T)
sum_ <- path[[1]][[1]][2]
n_ <- path[[1]][[1]][1]
id_ <- 1
if (length(path)>1){
cat( " path: ",sum_-n_,"+",sep="")
write(paste0(" path: ",sum_-n_,"+",sep=""),file = output_file_csv,append=T)
for (i in 2:(n-1) ) {
sum_ <- map(path[[i]],2) %>% unlist()
id_ <- which(sum_==n_)
if (length(id_)>1) {
id_ <- id_[1]
}
n_ <- map(path[[i]],1) %>% unlist() %>% .[id_]
cat( sum_[id_]-n_ , "+",sep="")
write(paste0( sum_[id_]-n_ , "+",sep=""),file = output_file_csv,append=T)
if (i==(n-1)) {
cat( n_,"\n")
write(paste0( n_,"\n"),file = output_file_csv,append=T)
}
}
} else {
cat( " path: ",sum_-n_,"+",n_,"\n",sep="")
write(paste0(" path: ",sum_-n_,"+",n_,"\n",sep=""),file = output_file_csv,append=T)
}
}
}
```