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 iwo.malyszka
import math
def checkio(height, width):
typ = None
if height > width:
typ = 'prolate'
elif width > height:
typ = 'oblate'
else:
typ = 'sphere'
volume = 0.0
area = 0.0
a = float(width/2)
c = float(height/2)
if typ == 'prolate':
E = 1 - ((a**2)/(c**2))
area = 2 * math.pi * (a**2) * (1 + (c/(a*math.sqrt(E))) * math.asin(math.sqrt(E)))
elif typ == 'oblate':
E = 1 - ((c**2)/(a**2))
area = 2 * math.pi * (a**2) * (1 + ((1-E)/math.sqrt(E)) * math.atanh(math.sqrt(E)))
else:
area = 4 * math.pi * ((height/2)**2)
volume = ((4 * math.pi) / 3) * ((width/2)**2) * (height/2)
return [round(volume, 2), round(area, 2) ]
#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"
Nov. 7, 2016