How much present did you get for Christmas?
Jelly, 19 18 bytes
Zµ*3×1420÷339Ḣo@PS
Try it online!
Unfortunately, Jelly does not have a π constant yet, and the vectorizer doesn't handle floats properly.
To overcome these issues, instead of multiplying by 4π/3, we multiply by 1420 and divide by 339. Since 1420 ÷ 339 = 4.18879056… and 4π/3 = 4.18879020…, this is sufficiently precise to comply with the rules.
The newest version of Jelly could accomplish this task in 14 bytes, with better precision.
Zµ*3×240°Ḣo@PS
Try it online!
How it works
Zµ*3×1420÷339Ḣo@PS Left argument: A, e.g., [[1, 2, 3], [4, 0, 0]]
Z Zip A; turn A into [[1, 4], [2, 0], [3, 0]].
µ Begin a new, monadic chain with zip(A) as left argument.
*3 Cube all involved numbers.
×1420 Multiply all involved numbers by 1420.
÷339 Divide all involved numbers by 339.
This calculates [[4.19, 268.08], [33.51, 0], [113.10, 0]]
Ḣ Head; retrieve the first array.
This yields [4.19, 268.08].
P Take the product across the columns of zip(A).
This yields [6, 0].
o@ Apply logical OR with swapped argument order to the results.
This replaces zeroes in the product with the corresponding
results from the left, yielding [6, 268.08].
S Compute the sum of the resulting numbers.
The non-competing version uses ×240°
instead of ×1420÷339
, which multiplies by 240 and converts the products to radians.
Pyth, 19 18 bytes
sm|*Fd*.tC\ð7^hd3Q
1 byte thanks to Dennis
Demonstration
Input format is list of lists:
[[1,4,3],[2,2,2],[3,0,0],[4,4,4]]
It simply multiplies the dimensions together to calculate the cube volume. If that comes out to zero, it calculates the sphere volume.
The sphere constant, 4/3*pi
is calculated as 240 degrees in radians. .t ... 7
converts an input in degrees to radians, and C\ð
calculates the code point of ð
, which is 240.
Haskell, 40 bytes
q[x]=4/3*pi*x^^3
q x=product x
sum.map q
Usage example: sum.map q $ [[1,4,3],[2,2,2],[3],[4,4,4]]
-> 197.09733552923254
.
How it works: For each element of the input list: if it has a single element x
calculate the volume of the sphere, else take the product
. Sum it up.