Tuesday, February 2, 2021

IslandT: The 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.

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!



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...