- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想问你对以下场景的不同看法:假设我们有几个列表,应该对那些不为空的列表执行一些操作:
if l1 or l2 or l3 or l4 or l5 or l6 or l7 or l8 or l9:
print 'we have to do something in one or more lists'
if l1:
print 'l1'
f1()
if l2:
print 'l2'
f2()
if l3:
print 'l3'
f3()
if l4:
print 'l4'
f4()
if l5:
print 'l5'
f5()
if l6:
print 'l6'
f6()
if l7:
print 'l7'
f7()
if l8:
print 'l8'
f8()
if l9:
print 'l9'
f9()
代码本身简单易懂,但这会引发 (12) 的 Mccabe 复杂度值。要降低此值,您将如何处理它?我很乐意听听您的想法。
提前谢谢你。
更新:
准确地想象一下这个具体情况。你怎么能接近它?:
if a:
A = True
if b:
B = True
if c:
C = True
if d:
D = "D"
if e:
E = "E"
if f:
F = "F"
我认为,在这种情况下,创建 6 个不同的函数效率不高而且python...
最佳答案
使用列表列表和函数列表
def f1(l1):
# do whatever is required to the l1 list
def f2(l2):
# similarly
# and similarly for f3 .. f9
...
lofl = [ l1, l2, l3, l4, l5, l6, l7, l8, l9 ]
loff = [ f1, f2, f3, f4, f5, f6, f7, f8, f9 ]
for f, l in zip( loff, lofl):
if l: # if the functions f cannot themselves safely do nothing for a falsy argument
f( l)
希望所需的函数数量少于九个(在本例中)。您还可以轻松地向函数传递一个参数,这样一个函数就可以是通用的,并告诉它在调用时要执行什么变体操作
for f, l, p in zip( loff, lofl, lofp): # or, zip( loff, lofl, list(range(9)) )
f(l, p)
或者甚至向函数传递任意一组关键字参数
lofargs=[ { 'foo':1, 'bar':'Monty' }, # kwargs for f1
{ 'foo':2, 'bar':'Python'},
{ 'foo':3 },
{}, # no kwargs at all for f4,
...
]
for f, l, k in zip( loff, lofl, lofargs):
f( l, **k )
几年后重温这一点,我现在全力以赴地采用目录方法:
callstruct = [
{ 'func':f1, 'arg':l1, 'foo':1, 'bar':'Monty' },
{ 'func':f2, 'arg':l2, 'foo':2, 'bar':'Python'},
{ 'func':f3, 'arg':l3, 'foo':3 },
{ 'func':f4, 'arg':l4}, # no kwargs at all for f4,
...
]
for d in callstruct:
func = d.pop('func')
arg = d.pop('arg')
func( arg, **d)
如果有任何外部循环,请注意 .pop
会改变 callstruct
中的字典,因此您可能希望从 dd = d.copy 开始()
并在 dd 上工作。
不知道为什么我现在有这种感觉。也许太多的 JavaScript 曝光?我经常在我最近的代码中找到这样的单行代码。比许多 if
x = {'foo':1, 'bar':2, 'baz':3}.get(choice, DEFAULT) # or ERROR, or catch KeyError
关于python - 避免多个 IF 以确保 Mccabe 复杂性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52850003/
我正在尝试将游戏中的 GPS map 系统复制到网络中。基本上游戏中有 12 张 map ,每张 map 都有不同的安全区域,你可以在其中扭曲你的 Angular 色或走动,但我一路上遇到了一些问题。
给定一个根目录,逐行读取 rootDirectory 或子目录中的所有文件,并对每个文件中的所有数字求和。每个文件的每一行都有一个编号。所以我只需要读取所有文件并对所有数字求和并返回。我想出了下面的代
我使用的是 MySQL 5.5,有两个表 T1(ID, NAME) 和 T2(ID, MARKS),下面是表中的数据。 T1的数据 ID NAME 1 A 2 B 3 C T2的数据 ID MA
我必须确定以下函数的时间复杂度(大 O): void BET::makeEmpty(BinaryNode* &n) { if(n != NULL) { makeEmpt
我基本上了解如何计算函数的复杂度。这同样适用于确定数学函数的增长顺序。 [我可能不像我想的那样理解它,这就是为什么我可能会问这个。] 例如: an^3 + bn^2 + cn + d 可以用大 O 表
我有一个家庭作业问题,一段时间以来我一直在努力解决,但我终其一生都无法解决。 我有一张尺寸为 X*Y 的纸和一组尺寸较小的图案,以及与之相关的价格值。我可以水平或垂直切割板材,我必须找到优化的切割模式
我写了这个字符串所有排列的解决方案。我对这个解决方案的时间和空间复杂性有疑问。我假设时间复杂度为 O(n³),因为嵌套循环和递归以及空间复杂度为 O(n),因为递归。 我的假设是否正确?如果可以,有没
刚遇到这个问题: 子集求和问题:求给定数组中两对数字的总和等于给定数字的个数 例如:给定总和为 9,数组为 { 0, 1, 2, 7, 13 } => O/P 为 1 对(2 和 7) 似乎这可以在
鉴于我有一个包含一组单词的文件: 1) 如果我选择一个哈希表来存储单词 -> 计数,查找特定单词出现的时间复杂度是多少? 2) 我怎样才能按字母顺序返回这些单词? 如果我选择哈希表,我知道 1) 的时
我很难理解大 O 时间复杂度。 Big O 的正式定义: f(n) = O(g(n)) means there are positive constants c and k, such that 0
有人知道 ECMAScript5 的 Object.keys() 在常见实现中的时间复杂度吗? n 个键的时间复杂度是 O(n) 吗?假设采用哈希实现,时间与哈希表的大小成正比吗? 我正在寻找语言实现
我需要知道,就复杂性而言,什么更好。要么为每个按钮从 xml 中标识一个单独的 onClick 方法,如下所示: android:onClick:"clickHandler" 和java代码: pub
无论如何我都不是 Javascript 的新手;也就是说,我对 bind 的作用及其行为方式有了基本的了解。但是,我遇到了一个我对结果有点不确定的用例。让我详细说明一个示例设置: 示例设置 首先,我有
这个问题在这里已经有了答案: Python list.clear() time and space complexity? (4 个答案) 关闭 1 年前。 Python 3 方法 list.cle
我创建的这个计算两个数组的交集是线性的方法的复杂度(在良好、平均、最差的情况下)? O(n) public void getInt(int[] a,int[] b){ int i=0; int
假设我已经使用 PriorityQueue 实现了 dijkstras,因此在未访问的节点中添加和删除需要 O(log n)。 PQ 最多包含 E 个节点,因此清空它我们得到 O(E)。当 PQ 不为
我能得到一些帮助来理解如何解决这个教程问题吗?我仍然不明白教授的解释。我不确定如何计算第三个/最内层循环的大 0。她解释说,该算法的答案是 O(n^2),并且第二个和第三个循环必须被视为具有 O(n)
有人可以告诉我这个过程在 for 迭代中的时间复杂度吗?这段代码是FloydWarshall算法的“重构路径”部分。prev[n][n]是最短路径中源节点和目的节点之间的节点矩阵。printAllSP
运行时间、复杂性、编译时间和执行时间有什么区别? 运行时间与时间复杂度有冲突,执行时间和执行时间有什么区别? 最佳答案 您真正需要的是如何将大O时间复杂度转换为运行时。这不像一开始看起来那么容易。 因
我对算法复杂度的计算很困惑。对于一项任务,我们被赋予以下功能并要求找到它的复杂性。 int selectkth(int a[], int k, int n) { int i, j, mini,
我是一名优秀的程序员,十分优秀!