+ 11

How does the following program work? How does max(list) and min(list) work with strings?

https://code.sololearn.com/cSsS4133xXhM/?ref=app

21st Jun 2019, 6:55 AM
SWETA X
SWETA X - avatar
6 Respostas
+ 5
lowercase letters are higher than uppercase letters. ord('i') = 105 and ord('P') = 80
21st Jun 2019, 7:41 AM
Mert Yazıcı
Mert Yazıcı - avatar
+ 10
HonFu Thanks, I get it now!🤗
21st Jun 2019, 8:42 AM
SWETA X
SWETA X - avatar
+ 8
HonFu So, max(list) gives the result as 'is'. Is 'i' > 'p' according to unicode??
21st Jun 2019, 7:09 AM
SWETA X
SWETA X - avatar
+ 5
Yeah, the order is not always what you expect because the decisive factor is the unicode number. If you want to compare alphabetically and ignore cases, you'd have to lower what you compare, for example 'pYthOn'.lower() == 'PyThoN'.lower(). And if your language has special letters like ä, ö, ü or something, sorting by size can become a bit more complicated.
21st Jun 2019, 8:30 AM
HonFu
HonFu - avatar
+ 4
The first two letters of the words are compared. If one is bigger (e. g. 'b' > 'a', unicode being the table to check it), the comparison is already finished. So 'ba' is bigger than 'ab' because the first letter already decides. If the letters are equal, comparison moves on to the next pair of letters. And so on. max and min look for the largest or smallest in the list according to the standard I described.
21st Jun 2019, 7:04 AM
HonFu
HonFu - avatar