您的位置:首页 > 其它

R4fun之文字竖排

2015-06-04 23:29 411 查看
Crossin的编程教室中看到一则文字竖排的题目(http://chuansong.me/n/265078)


于是思量着应该如何实现,就顺手写了个R实现的demo以供借鉴

<span style="font-size:18px;">verticalP<-function(str,row){
len=nchar(str)
if(len%%row!=0){
column=len%/%row+1
}else{
column=len%/%row
}
sum=column*row
chars=c(strsplit(str,"")[[1]],rep(' ',sum-len))
mat=matrix(chars,ncol=column)
pivot=column%/%2
for(i in seq(pivot)){
temp=mat[,i]
mat[,i]=mat[,column-i+1]
mat[,column-i+1]=temp
}
result=apply(mat,1,paste,collapse='|')
cat(result,sep='\n')
}</span>经测试尚可运行良好

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: