-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryTree_MDPyramid_v0.3_test_.R
100 lines (93 loc) · 2.89 KB
/
binaryTree_MDPyramid_v0.3_test_.R
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
# https://rstudio.cloud/project/152401
rm(list = ls())
gc()
pacman::p_load(tidyverse, data.table, stringr, magrittr,glue,gdata)
#----------------------------------------------------------------------------
#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
}
#as.integer customized
as.integer0 <- function(x){
x <- as.integer(x)
if (length(x)==0 ) x=-Inf
else x=x
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))
}
#----------------------------------------------------------------------------
set.seed(123)
#data setup
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)
}
}
}