Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Humpty Dumpty Form by cbrunet
from math import asin, atanh, pi, sqrt
def checkio(height, width):
""" Calculates volume and surface of a spheroid
height: height of the spheroid
width: width of the spheroid
return: list [volume, surface]
"""
a = width / 2 # equatorial radius
c = height / 2 # distance from center to pole
V = 4 * pi * a * a * c / 3
if c < a: # prolate spheroid
e = sqrt(1 - (c / a)**2)
S = 2 * pi * a * a * (1 + (1 - e * e) * atanh(e) / e)
elif c > a: # oblate spheroid
e = sqrt(1 - (a / c)**2)
S = 2 * pi * a * a * (1 + c * asin(e) / a / e)
else: # sphere
S = 4 * pi * a * a
return [V, S]
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio(4, 2) == [8.38, 21.48], "Prolate spheroid"
assert checkio(2, 2) == [4.19, 12.57], "Sphere"
assert checkio(2, 4) == [16.76, 34.69], "Oblate spheroid"
Feb. 7, 2014
Comments: