i have this code and when it raise exception i investigated and check max(xs) > 1000
when max(xs) = 'i' it evaluated to True. I do not understand why and how. Please help me to understand this code. I got it online and needed to understand it well before i can use it .
Thanks in advance
def solution(xs):
maxNeg = -9999999
count = 0
countNeg = 0
res = str(int(1))
print res
print 'res type: ',type(res)
if len(xs) > 50 or len(xs) < 1 or max(xs) > 1000 or min(xs) < -1000:
raise ValueError("Invalid inputs")
solution([2, 0, 2, 'i'])
#solution([-2, -3, 4, -5])
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
>>> 'i' > 1000
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
'i' > 1000
TypeError: '>' not supported between instances of 'str' and 'int'
Got it “where” though? That’s some older Python version…
Python nowadays uses print().
Also I just checked and in my version the sys.sizeof("i") returns 26, so there are some major differences.
If you aren’t paying for this challenge, I would stop doing it. There is minimal value in learning Python 2. It is no longer in use.
As of January 1st, 2020 no new bug reports, fixes, or changes will be made to Python 2, and Python 2 is no longer supported.
If people find catastrophic security problems in Python 2, or in software written in Python 2, then most volunteers will not help fix them. If you need help with Python 2 software, then many volunteers will not help you, and over time fewer and fewer volunteers will be able to help you. You will lose chances to use good tools because they will only run on Python 3, and you will slow down people who depend on you and work with you.
Your points are very accurate and clear. I personally wonder why google chose python 2.7 for their foobar challenge which is the challenge i talked about.
Because that challenge is not being maintained in any way and was written when Python 2 was not quite dead yet. Google no longer recruits/hires through the foobar challenge.
Documentation of python 2 sums this in a bit funny way:
Objects of different types, except different numeric types and different string types, never compare equal; such objects are ordered consistently but arbitrarily
So I wouldn’t try to understand why exactly one type is greater than another, but even more I’d try to not compare different types.