cosmologicon / pywat

Python wats
1.22k stars 99 forks source link

Question 5: zero sum is "possible" ? #39

Open mishgu opened 6 years ago

mishgu commented 6 years ago

So we have that question

>>> x, y = ???
 >>> sum(0 * x, y) == y
 False

if x equal one of

[], '' or ()

and

y = float('nan')

that snippet is possible. For ex:

>>> x, y = '', float('nan')
>>> sum(0 * x, y) == y
False

Please correct me if I'm wrong.

pschwede commented 6 years ago

Let's break it down a bit:

0*[] means [], because 2*[x] means [x,x]. If you sum([], float("nan")), you get the sum of all numbers in [] plus your starting value float("nan"), which results in nan. However if you try float("nan") == float("nan") you always get False. That's I think because == either compares two numbers or two objects with each other (despite being typed float). Now, because float("nan") yields not a number, it cannot be equal to any number, not even to itself:

>>> y=float("nan")
>>> y==y
False

In the end, why did you use == on not a number in the first place? I don't see where this is a pywat.

mishgu commented 6 years ago

When we look at Question 5: zero sum the answer tells "No. This snippet is impossible." So if use my example, that snippet is possible and can I use that example as correct answer

cosmologicon commented 6 years ago

Thanks for the report. But you can't use NaNs. Those are considered "trick" answers for the purpose of the quiz, because NaNs have edge cases that have nothing to do with Python, and it would be unfair to expect people to know them all. This is the first bullet point under the "Details and Scope" section. If you think there's a clearer way to say this, I'm open to it.