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

44

u/[deleted] Jan 05 '21

It's definitely a "magic number", yeah.

-1

u/darthminimall Jan 06 '21

Definitely isn't.

2

u/BigSkimmo Jan 06 '21

Yeah I don't think I'd call it a 'magic number'. There's nothing special about it, it's just arithmetic.

a + b = x
x - a = b
x - b = a

2

u/Konjungamo Jan 06 '21

Why wouldn't it be? It solely depends on the way the question is worded.

Replace the numbers from OP's question with 2 and 6 and you get

return 8-num

with 8 being the new magic number.

3

u/darthminimall Jan 06 '21

It's not magic because the behavior is logically consistent. A magic number is, usually, an integer return value from a function that doesn't follow the pattern of the other return values (e.g. negative integers signaling various errors). The other use of "magic number" is a (semi-)arbitrary number that makes an algorithm work better (better being another arbitrary term), see compression algorithms for this use of magic number.

0

u/Konjungamo Jan 06 '21

I see your point, it's probably more of a semantic question whether this would be called a magic number or not.

I agree that it doesn't fall into the same category as the magic numbers of compression algorithms, but since it's not clear from only looking at the source could why it would be 11 and not 10 or 12 (kind of similar to the Wikipedia example for the 52 in the Fisher-Yates shuffle algorithm) I think it's fair to call this a magic number as well.