AS3 求大神优化下这段代码,这个代码里的if else太多了,但需要去判断关数在返回指定的数组,求大神简化!

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AS3 求大神优化下这段代码,这个代码里的if else太多了,但需要去判断关数在返回指定的数组,求大神简化!相关的知识,希望对你有一定的参考价值。

public static function arrNum(level:int):Array

var a:int = Math.floor(1 + Math.random() * 9);
var b:int = Math.floor(10 + Math.random() * 20);
var c:int = Math.floor(2 + Math.random() * 3);
var d:int = Math.floor(2 + Math.random() * 6);
var e:int = Math.floor(15 + Math.random() * 3);
var arr:Array
if (level == 1)//1

arr = new Array(a, a + 1, a + 2, a + 3, a + 4) ;
else if (level == 2) //5原关数,下同
arr = new Array(b, b + 1, b, b + 3, b);
else if (level == 3) //2
arr = new Array(b, b + 2, b + 6, b + 8, b + 12);
else if (level == 4) //3
arr = new Array(c, c * 6, c * 36, c * 216, c * 1296);
else if (level == 5) //4
arr = new Array(a, a+b, 2*a+b, 3*a+2*b, 5*a+3*b);
else if (level == 6) //6
arr=new Array(Math.pow(Math.pow(c,2),2),Math.pow(Math.pow(c+1,2),2),Math.pow(Math.pow(c+2,2),2),Math.pow(Math.pow(c+3,2),2),Math.pow(Math.pow(c+4,2),2))//n平方后在平方,底数平方的平方
else if (level == 7) //7要改
arr = new Array(c, Math.pow(c+1, 3)-1, Math.pow(c+2, 3)-1, Math.pow(c+3, 3)-1, Math.pow(c+4, 3)-1);//n的m³-1
else if (level == 8) //8
arr=new Array(Math.pow(d,2)-1,Math.pow(d+1,2)-1,Math.pow(d+2,2)-1,Math.pow(d+3,2)-1,Math.pow(d+4,2)-1)//原公式:Math.pow(n,2)-1

return arr

使用switch 参考技术A 技能追问

????

求python大神解释下这段代码,没接触过python不会啊

def __init__(self,
n_estimators=10,
criterion="gini",
max_depth=None,
min_samples_split=2,
min_samples_leaf=1,
min_weight_fraction_leaf=0.,
max_features="auto",
max_leaf_nodes=None,
bootstrap=True,
oob_score=False,
n_jobs=1,
random_state=None,
verbose=0,
warm_start=False,
class_weight=None):
super(RandomForestClassifier, self).__init__(
base_estimator=DecisionTreeClassifier(),
n_estimators=n_estimators,
estimator_params=("criterion", "max_depth", "min_samples_split",
"min_samples_leaf", "min_weight_fraction_leaf",
"max_features", "max_leaf_nodes",
"random_state"),
bootstrap=bootstrap,
oob_score=oob_score,
n_jobs=n_jobs,
random_state=random_state,
verbose=verbose,
warm_start=warm_start,
class_weight=class_weight)

self.criterion = criterion
self.max_depth = max_depth
self.min_samples_split = min_samples_split
self.min_samples_leaf = min_samples_leaf
self.min_weight_fraction_leaf = min_weight_fraction_leaf
self.max_features = max_features
self.max_leaf_nodes = max_leaf_nodes

这就是一段构造函数。
(self,
n_estimators=10,
criterion="gini",
max_depth=None,
min_samples_split=2,
min_samples_leaf=1,
min_weight_fraction_leaf=0.,
max_features="auto",
max_leaf_nodes=None,
bootstrap=True,
oob_score=False,
n_jobs=1,
random_state=None,
verbose=0,
warm_start=False,
class_weight=None):
这是构造函数的参数,有默认值。

super(RandomForestClassifier, self).__init__(
base_estimator=DecisionTreeClassifier(),
n_estimators=n_estimators,
estimator_params=("criterion", "max_depth", "min_samples_split",
"min_samples_leaf", "min_weight_fraction_leaf",
"max_features", "max_leaf_nodes",
"random_state"),
bootstrap=bootstrap,
oob_score=oob_score,
n_jobs=n_jobs,
random_state=random_state,
verbose=verbose,
warm_start=warm_start,
class_weight=class_weight)

supper会调用基类构造函数,你可以认为这一串就是基类构造函数的参数。

self.criterion = criterion
self.max_depth = max_depth
self.min_samples_split = min_samples_split
self.min_samples_leaf = min_samples_leaf
self.min_weight_fraction_leaf = min_weight_fraction_leaf
self.max_features = max_features
self.max_leaf_nodes = max_leaf_nodes

这一串就是属性赋值。追问

你好,十分感谢你的回答,base_estimator=DecisionTreeClassifier(),
这一个代码是什么意思呢请问

参考技术A import re
reader = open('test.txt','r')
for txt in reader.readlines():
p = r'([a-z]+|[A-z]+)'
#print re.findall(p,txt)
pattern =  re.compile(p)
match = pattern.match(txt)
if match:
print match.groups()

以上是关于AS3 求大神优化下这段代码,这个代码里的if else太多了,但需要去判断关数在返回指定的数组,求大神简化!的主要内容,如果未能解决你的问题,请参考以下文章

求python大神解释下这段代码,没接触过python不会啊

js问题,动态增加select里的option,求问为啥这段代码不执行?求大神指教!

在java 事件处理中,我这段代码为啥就是没有响应啊!!编译也通过了,求大神指点啊 !!!!!!!

关于js中 .map()的问题,希望大神可以帮忙解读下这段代码的实现过程

excel函数,如图,满足在8点和11点这段时间内的上班人员的工作时长的和,求大神公式如何写

C++ 字符串匹配的多if...if else...if else优化