码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-09-24 13:51:41      阅读:102      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!