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.
The solution:
Loop through each list and find the volume, then subtracts those two volume and returns their absolute value!
def find_difference(a, b):
v1 = 1
v2 = 1
for e in a:
v1 *= e
for e in b:
v2 *= e
return abs(v1-v2)
This is it! If you have a better solution kindly let me know about it!
Please follow and like us:
With Python 3.8+, you may also do it like this:
from math import prod
def find_difference(a, b):
return abs(prod(a) – prod(b))