May-07-2018, 05:54 AM
(May-07-2018, 05:38 AM)buran Wrote: actually, the easiest way would be to add sorted to your line 5 list comprehension
a = [sorted(input().split()) for i in range(n)]this way you always get the number as element with index 0 because when sorting '0' to '9' always go before 'A'
def parse_data(item): return (int(item[0]), item[1]) data = ['red 10', '10 blue', 'green 7'] data = [parse_data(sorted(item.split())) for item in data] output = [cup for radius, cup in sorted(data)] print('\n'.join(output))note that sample output in your assignment is NOT correct
It is correct because if it's in the format number color, then number need to be divided by 2 so it will be 5.
(May-06-2018, 02:26 PM)Gribouillis Wrote:(May-06-2018, 11:42 AM)Otbredbaron Wrote: Why do we need to be explicit and write except ValueError?I'm not sure I understand exactly what you mean, but here I know that ifi[1]
contains anything that is not convertible to an integer the call toint()
will raise ValueError. In such a case, I always catch the exact exception because I don't want to catch anything else. For example if for some reason there is noi[1]
, python will throwKeyError
and I don't want to catch this at this point. For debugging purposes, it is always better to let unexpected exceptions propagate.
I see now, I need to get used to this try except thing.