任意排列、组合终极Shell脚本

六眼飞鱼酱① 提交于 2020-02-29 08:13:54

作者:crulat 永夜

#!/bin/bash
# permutation_combination.sh

arg0=-1
number=${2}
eval ary=({1..${1}})
length=${#ary[@]}
output(){ echo -n ${ary[${!i}]}; }
prtcom(){ nsloop i 0 number+1 output ${@}; echo; }
percom(){ nsloop i ${1} number${2} ${3} ${4} ${5}; }
detect(){ (( ${!p} == ${!q} )) && argc=1 && break 2; }
invoke(){ echo $(percom ${argu} nsloop -1) ${para} $(percom ${argu}); }
permut(){ echo -n "${1} arg${i} ${2} "; (( ${#} != 0 )) && echo -n " length "; }
nsloop(){ for((${1}=${2}+1; ${1}<${3}; ++${1})); do eval eval \\\$\{{4..${#}}\}; done; }
combin(){ (( ${#} != 0 )) && echo -n "${1} arg$((i+1)) arg${i} length " || echo -n "arg$((i+1)) "; }
prtper(){ argc=0; nsloop p 0 number+1 nsloop q p number+1 detect ${@}; (( argc == 1 )) && return; prtcom ${@}; }

case ${3} in
     p)para=prtper
       argu="-0 +1 permut" ;;
     c)para=prtcom
       argu="-1 +0 combin" ;; 
esac

$(invoke)


Invoke the script:
C 4 3
[root@node2 ~]# bash permutation_combination.sh 4 3 c
123
124
134
234

P 4 3
[root@node2 ~]# bash permutation_combination.sh 4 3 p | xargs -n 6
123 124 132 134 142 143
213 214 231 234 241 243
312 314 321 324 341 342
412 413 421 423 431 432

C 8 5
[root@node2 ~]# bash permutation_combination.sh 8 5 c | xargs -n 8
12345 12346 12347 12348 12356 12357 12358 12367
12368 12378 12456 12457 12458 12467 12468 12478
12567 12568 12578 12678 13456 13457 13458 13467
13468 13478 13567 13568 13578 13678 14567 14568
14578 14678 15678 23456 23457 23458 23467 23468
23478 23567 23568 23578 23678 24567 24568 24578
24678 25678 34567 34568 34578 34678 35678 45678

P 5 5
[root@node2 ~]# bash permutation_combination.sh 5 5 p | xargs -n 12
12345 12354 12435 12453 12534 12543 13245 13254 13425 13452 13524 13542
14235 14253 14325 14352 14523 14532 15234 15243 15324 15342 15423 15432
21345 21354 21435 21453 21534 21543 23145 23154 23415 23451 23514 23541
24135 24153 24315 24351 24513 24531 25134 25143 25314 25341 25413 25431
31245 31254 31425 31452 31524 31542 32145 32154 32415 32451 32514 32541
34125 34152 34215 34251 34512 34521 35124 35142 35214 35241 35412 35421
41235 41253 41325 41352 41523 41532 42135 42153 42315 42351 42513 42531
43125 43152 43215 43251 43512 43521 45123 45132 45213 45231 45312 45321
51234 51243 51324 51342 51423 51432 52134 52143 52314 52341 52413 52431
53124 53142 53214 53241 53412 53421 54123 54132 54213 54231 54312 54321


python 有 itertools 排列组合包可以使用:

python的数学函数(1)-python组合函数模块itertools

http://shenguanpu.blogbus.com/logs/155452282.html

Python性能鸡汤

http://www.oschina.net/question/1579_45822

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!