标签:ref min bsp dimens sed HERE therefore oid imp
In this simple exercise, you will create a program that will take two lists of integers, a
and b
. Each list will consist of 3 positive integers above 0, representing the dimensions of cuboids a
and b
. You must find the difference of the cuboids‘ volumes regardless of which is bigger.
For example, if the parameters passed are ([2, 2, 3], [5, 4, 1])
, the volume of a
is 12 and the volume of b
is 20. Therefore, the function should return 8
.
Your function will be tested with pre-made examples as well as random ones.
If you can, try writing it in one line of code.
def find_difference(a, b): # Your code here! x = a[0]*a[1]*a[2] y = b[0]*b[1]*b[2] return max(x, y)-min(x, y)
其他的办法:
from numpy import prod def find_difference(a, b): return abs(prod(a) - prod(b))
def find_difference(a, b): return abs((a[1]*a[2]*a[0])-b[1]*b[2]*b[0])
1、绝对值使用abs()
2、list里面值的乘积使用prod(list),需要导入,from numpy import prod
【Kata Daily 190924】Difference of Volumes of Cuboids(长方体的体积差)
标签:ref min bsp dimens sed HERE therefore oid imp
原文地址:https://www.cnblogs.com/bcaixl/p/11577615.html