Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - arranging words in alphabetical order

The program must print the name which is alphabetically the last one out of 8 elements. The names/words can be inputted in any way through code. I think I should be using lists and in range() here. I had an idea of comparing the first/second/third/... letter of the input name with the letters of the previous one and then putting it at the end of the list or in front of the previous one (depending on the comparison), and then repeating that for the next name. At the end the program would print the last member of the list.

like image 815
CrashZer0 Avatar asked Dec 10 '12 21:12

CrashZer0


1 Answers

If you have a mix of capitalized words and lowercase words you could do this:

from string import capwords     

words = ['bear', 'Apple', 'Zebra','horse']

words.sort(key = lambda k : k.lower())

answer = words[-1]

Result:

>>> answer
'Zebra'
>>> words
['Apple', 'bear', 'horse', 'Zebra']
like image 109
Akavall Avatar answered Oct 07 '22 14:10

Akavall