Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Revisions

2 of 2
Commonmark migration

Sage, 247 bytes

def f(p):
 for i in[1,2,4]:
 z=[i&1,i&2,i&4,0,0,0]
 M=matrix([z]+[[x*x,x*y,y*y,x,y,1]for x,y in p])
 try:A,B,C=(M\vector(z))[:3]
 except:continue
 d=B*B-4*A*C
 return['parabola','hyperbola','circle','ellipse'][[d==0,d>0,d<0and B==0and A==C,d<0].index(1)]

Try it online

This function takes an iterable of (x,y) pairs as input, tries computing the discriminant of each of the 3 possible linear systems (A=1, B=1, and C=1), and outputs the type of conic section based on the values of the discriminant, A, B, and C.

There's probably some more golfing to be done, but I'm rusty with Sage and sleepy right now, so I'll work on it more in the morning.

user45941

AltStyle によって変換されたページ (->オリジナル) /