Skip to content

Instantly share code, notes, and snippets.

@ChlorUpload
Created April 13, 2020 17:55
Show Gist options
  • Save ChlorUpload/a7d1c6eb7d28c5dbc1ab25c62d5efe54 to your computer and use it in GitHub Desktop.
Save ChlorUpload/a7d1c6eb7d28c5dbc1ab25c62d5efe54 to your computer and use it in GitHub Desktop.
Get Area of Quadratic function
def IntDefault(s : str, default : int):
if s == '':
return default
else:
return int(s)
def QFarea(d : str):
# Toknization (White Spacing kill)
d = d.replace(' ', '')
# Exception Handling
if 'x^2' not in d:
if 'x' not in d:
print('Not available type: constant function')
return None
else:
print('Not available type: linear function')
return None
# Quadratic term Coefficient Parse
s = d.split('x^2') # ['y=-5', '+3x-5']
quad_coeff = s[0].replace('y=', '') # ['y=-5', .. ] -> '-5'
quad_coeff = IntDefault(quad_coeff, 1)
# Linear term Coefficient, Constant term Parse
if 'x' in s[1]: # s[1] = '+3x-5'
sp = s[1].split('x') # ['+3', '-5']
lin_coeff = sp[0]
const_coeff = sp[1]
lin_coeff = IntDefault(lin_coeff, 1)
const_coeff = IntDefault(const_coeff, 0)
else:
lin_coeff = 0
const_coeff = 0
# Only Const term parse
if s[1] != '':
const_coeff = s[1]
const_coeff = IntDefault(const_coeff, 0)
if lin_coeff**2 - 4*quad_coeff*const_coeff <= 0:
print('Not available type: Area cannot be defined')
else:
dif = (lin_coeff**2 - 4*quad_coeff*const_coeff)**0.5 / quad_coeff
print('Area : ', dif ** 3 / 6)
QFarea("y = x^2- 6x")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment