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 jtarnowska
import math
def surfoblate(h, w):
c = h/2
a = w/2
e = math.sqrt(1- ((c**2) / (a**2)))
surfob = 2 * math.pi * (w/2)**2 * (1 + (1 - e**2) / e * math.atanh(e) )
return surfob
def surfprolate(h, w):
c = h/2
a = w/2
e = math.sqrt(1- ((a**2) / (c**2)))
surfpro = 2 * math.pi * (a**2) * (1+ (c * math.asin(e) / (a*e)))
return surfpro
def surfsphere(h, w):
r = h/2
surfsp = 4 * math.pi * (r**2)
return surfsp
def volume(h, w):
c = h/2
a = w/2
vol = 4/3 * math.pi * (a**2) * c
return vol
def volumesphere(h, w):
c = h/2
a = w/2
vol = 4/3 * math.pi * (a**3)
return vol
def checkio(height, width):
if height == width:
surf = surfsphere(height, width)
vol = volumesphere(height, width)
return [round(vol,2), round(surf,2)]
elif height < width:
surf = surfoblate(height, width)
vol = volume(height, width)
return [round(vol,2), round(surf,2)]
elif height > width:
surf = surfprolate(height, width)
vol = volume(height, width)
return [round(vol,2), round(surf,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"
Jan. 17, 2017