25 Jun 2012 15:33
List Methods, String Slices and For loops
Developer Ecofunds <ecofunds.developer <at> gmail.com>
2012-06-25 13:33:44 GMT
2012-06-25 13:33:44 GMT
Hello guys,
I'd like to ask you a little question about a script I did and isn't working properly.
It is one excercise from googles python classes <http://code.google.com/edu/languages/google-python-class/set-up.html>
I'm using python 2.5 because it's the one works with Google App Engine <https://developers.google.com/appengine/docs/python/gettingstarted/>
This is the problem:
##########################3
# B. front_x
# Given a list of strings, return a list with the strings
# in sorted order, except group all the strings that begin with 'x' first.
# e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields
# ['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
# Hint: this can be done by making 2 lists and sorting each of them
# before combining them.
# This is the code I did -- Romulo.
def front_x(words):
x_list = []
for string in words:
if string[0]=='x':
x_list.append(string)
words.remove(string)
sorted(words)
sorted(x_list)
x_list.extend(words)
return x_list
##############################
The problem with this code is that it only gets the first word beginning with x and the others remaisn on the original list, sorted at the end. I've tested on terminal many parts of the code and it whas fine, but when I run it complete, it does not work.
Following is the solution of the problem. I can understand it, I just can't understand why my code does not work.
#############################
def front_x(words):
x_list = []
other_list = []
for w in words:
if w.startswith('x'):
x_list.append(w)
else:
other_list.append(w)
return sorted(x_list) + sorted(other_list)
##############################
To download all the exercises, access: http://code.google.com/edu/languages/google-python-class/google-python-exercises.zip
Thank y'all.
Thank y'all.
_______________________________________________ Tutor maillist - Tutor <at> python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
RSS Feed