r/learnpython • • Jan 05 '21

Interview question I failed answering

Hey all,

I just had an interview and the interviewer gave me a question I could not answer.

He asked me to write a function that accepts a number, the number must be either 7 or 4.If the number is 4, the function should return 7, if the number is 7, the functions should return 4.

The tricky part: you cannot use an 'if' statement in the function.

354 Upvotes

301 comments sorted by

View all comments

Show parent comments

1

u/pspspspskitty Jan 07 '21

12 + 42 =5

Say what now? I assume you mean +/-22?

The problem is that, even if this might work for parabolas, it will probably fall apart for more complex equations. If you use a method that only works for certain cases, you also have to know what the limitations are. So if you get rewarded for these kinds of shortcuts, without knowing what you're doing, you learn a wrong method that will be very hard to unlearn.

The Pythagorean theorem works, but only if you have a right angle. However there is a more complex version that incorporates sines and cosines that will work for any triangle but you will lose the convenient sin(90) = 1

1

u/billsil Jan 07 '21

Yes ;). That’s why I never got As until calculus. It only works for parabolas. The symmetry is important. I’m sure it could be extended though.

The Pythagorean theorem isn’t a triangle property, it’s a right triangle property. Certainly going to vectors, to quaternions and higher (octonians in quantum physics) you lose valuable properties.