# I Cant Understand this python code and why it is True

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 .

``````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])
``````

Is this complete code? What error exactly are you getting?

Python canâ€™t compare integers with strings, `max(xs)` if `xs == [2, 0, 2, 'i']` would result in error.

1 Like

Yes it is. My worry is why
`'i' > 1000 #True`
as i was expecting it to throw and error, but

``````import sys
sys.getsizeof('i')                   #38
sys.getsizeof(1000)         # 24
``````

seems to be the reason.
Thanks

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.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (â€™).

``````>>> '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'
``````
1 Like

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.

Thanks a million times

I hope you tried it with python 2.7 as mine is precisely python 2.7.17

You should not be using Python 2. Python 2 is dead.

yes we are using python 2.7 not python 3.
It is required for the challenge i am taking. Thanks

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.

1 Like

Sure. I normally use python 3 but the challenge i am doing is tested using python 2 so i have to use same too.
Thanks

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.

Are you for real they no longer recruit through that challenge?

As far as Iâ€™m aware they havenâ€™t used this as a hiring tool since 2017ish. Python 2 knowledge isnâ€™t really something they value nowadays.

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.

https://docs.python.org/2/library/stdtypes.html#comparisons

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.