Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
math is not programming (4 lines)! solution in Clear category for Humpty Dumpty Form by CDG.Axel
from math import pi, asin, sqrt, log, asinh
def checkio(height, width):
# shortest but pile-upped
a, b = width / 2, height / 2
tmp = sqrt(max(a, b) ** 2 - min(a, b) ** 2)
tmp = a if a == b else b ** 2 / tmp * (asin(tmp / b) if b > a else asinh(tmp / b))
return [round(4 * pi * a * a * b / 3, 2), round(2 * pi * a * (a + tmp), 2)]
"""
def checkio(height, width):
# shorter but not too clear
a, b = height / 2, width / 2
sqm = sqrt(max(a, b) ** 2 - min(a, b) ** 2)
q = sqm / asin(sqm / a) if a > b else sqm / log((b + sqm) / a) if a < b else a
v = 4 / 3 * pi * a * b ** 2
s = 2 * pi * b * (b + a ** 2 / q)
res = [round(v, 2), round(s, 2)]
return res
def checkio(height, width):
# longest but easier to understand
a, b = height / 2, width / 2
v = 4 / 3 * pi * a * b ** 2
if a > b:
sqm = sqrt(a ** 2 - b ** 2)
s = 2 * pi * b * (b + a ** 2 / sqm * asin(sqm / a))
elif a < b:
sqm = sqrt(b ** 2 - a ** 2)
s = 2 * pi * b * (b + a ** 2 / sqm * log((b + sqm) / a))
else:
s = 4 * pi * a ** 2
res = [round(v, 2), round(s, 2)]
return res
"""
#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"
Sept. 20, 2021
Comments: