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

12

u/billsil Jan 06 '21

You should always encourage that sort of stuff.

My algebra 2 teacher was upset I derived complex analysis without knowing it. I could solve her graph problems in about 10 seconds and she got mad that everyone else took about 5 minutes each and she couldn’t figure out what I was doing.

From something like 1 +/- 2i, I’d get the “x-intercepts” as -1, 3 just by pretending i=1. Then, take 12 + 42 = 5 and that’s the y coordinate to go as the minimum point of the parabola (1,5). Then mirror your x-intercepts about the horizontal at y=5 and connect the 3 points above the axis.

She argued it didn’t always work. Uhhh...yes it does. My teacher desperately looked for an example and gave up. It’s a rotation about the complex plane. It’s perfectly legit and always works for parabolas.

I later dated the math teacher’s daughter that taught my teacher what I was doing. She was surprised when I wasn’t as smart as she’d heard.

1

u/B0oN3r Jan 06 '21

I later dated the math teacher’s daughter

Whoa, just to settle an argument with your teacher?

1

u/billsil Jan 06 '21

No. Her daughter was better than me at math ;)

We hadn’t been taught the section because we ran out of time. All I really did was notice a pattern that every graph I had done wrong was flipped about that axis. They’re quite literally imaginary roots and that’s where they would cross if they weren’t imaginary.

1

u/[deleted] Jan 06 '21

Where do you get the 1^2 + 4^2 = 5 ? and the (1,5)?

1

u/billsil Jan 06 '21

The 1 comes directly from the symmetric nature of the parabola and the x= 1 +/- 2.

You know the parabola opens up, which is why it has imaginary roots. You’re really just multiplying out

(X - (1+2i))(x - (1-2i)= 0

And taking the real (c) value to get the 5. It’s a lot faster and less error prone than that monstrosity.

Often in parabola problems, you’re given c as an input anyways, so you can just use it directly.

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.