【Kata Daily 190924】Difference of Volumes of Cuboids(长方体的体积差)

题目:

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

 

posted @ 2019-09-24 12:08  bcaixl  阅读(525)  评论(0编辑  收藏  举报