【r<-fun|【r<-fun|? 】用字符画出Z的曲线
5555555555
2n 2n 2n 2n 2n 2n 2n 2n 2n 2n
0a0a0a0a0a0a0a0a0a0a
ZDZDZDZDZDZDZDZDZDZD
h uh uh uh uh uh uh uh uh uh u
oooooooooo
【【r<-fun|【r<-fun|? 】用字符画出Z的曲线】实现的代码函数:
zcurve = function(string = NULL, n = NULL){
strs = stringr::str_split(string, pattern = "")
nInterval = n - 2
nTotal = n + nIntervalres = list()j = 1
for(s in strs){
nc = (n-1) * (length(s) %/% nTotal) + (length(s) %% nTotal %/% n) + 1
df = matrix(datahttps://www.it610.com/article/= "", nrow = n, ncol = nc)
for (i in 1:length(s)){
rx = ifelse((i %% nTotal <= n) & (i %% nTotal > 0), i %% nTotal,
ifelse(i %% nTotal == 0, 2, n - (i %% nTotal) %% n ))
ry = ifelse((i %% nTotal <= n) & (i %% nTotal > 0),
(n-1) * (i %/% nTotal) + 1,
ifelse((i %% nTotal <= n) & (i %% nTotal == 0),
(n-1) * (i %/% nTotal),
(n-1) * (i %/% nTotal) + i %% nTotal %% n + 1)
)df[rx, ry] = s[i]
}
res[[j]] = df
j = j + 1
}if(length(res) == 1){
prmatrix(res[[1]],
rowlab=rep("",nrow(res[[1]])),
collab=rep("",ncol(res[[1]])),
quote = FALSE)
}else{
for(i in 1:length(res)){
cat("#---- Z Curve #", i, "\n")
cat("\n")
prmatrix(res[[i]],
rowlab=rep("",nrow(res[[i]])),
collab=rep("",ncol(res[[i]])),
quote = FALSE)
cat("\n")
}
}}
支持多个字符串输入,通过设定输入的字符串和行数,函数就可以自动画出曲线了~
测试如下:
> zcurve(string = paste0(letters[1:20], collapse = ""), n = 5) aiq
bh jp r
cgkos
d fl nt
em
> zcurve(string = c(paste0(letters[1:20], collapse = ""),
+paste0(letters[20:1], collapse = "")), n = 5)
#---- Z Curve # 1 aiq
bh jp r
cgkos
d fl nt
em#---- Z Curve # 2 tld
sm ke c
rnjfb
q oi ga
ph
一开始的文字嘛,需要下面一行代码实现。
zcurve(string = paste0(rep(c("5","2","0","Z","h","o","u","D","a","n"),10),collapse = ""),
n = 6)
文章图片
推荐阅读
- 宽容谁
- 我要做大厨
- 增长黑客的海盗法则
- 画画吗()
- 2019-02-13——今天谈梦想()
- 远去的风筝
- 三十年后的广场舞大爷
- 叙述作文
- 20190302|20190302 复盘翻盘
- 学无止境,人生还很长