Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
john larson
16,594 PointsI'm always amazed at the clever ways that people solve problems, like this one
# the idea was to strip the extra u's off the end of the word
# I couldn't figure out how to do it
# but I found this code that makes me wonder
# how would anyone even think to do this?
word = "youuuu"
stripped = [l for i,l in enumerate(word) if l not in word[:i]]
print(stripped)
# or...
for i,l in enumerate(word):
if l not in word[:i]:
print(i,l)
# and it works perfectly :D
1 Answer
Alexander Davison
65,469 PointsFor this problem, I am going to strip off spaces only, not the u letter.
If you think about it, what a human might do is to start from the end of the string and remove the space until there are no more spaces at the end of the string.
Example:
# First, the string will start out as...
'abc '
# Next, since there's a space at the end of the string, you must remove that...
'abc '
# There's still spaces at the end, so you must remove AGAIN...
'abc '
# Et Cetera
'abc '
# And again, since there's STILL a space...
'abc'
# Notice how the last character in the string isn't a space anymore, but is the C letter. This tells us we should stop!
To write to code to do so, you can do this...
my_string = 'abc '
# If the last character of the string is a space, great, remove it.
# This runs repeatedly until there's no more spaces at the end of the string.
while my_string[-1] == ' ':
# Remove the last character of the string using slices
my_string = my_string[:-2]
print(my_string) # this will print "abc".
And, If you ignore the my_string == ..., print(...) and comments, the code turns out to be two lines of code!
The strategy for solving problems like this is to think:
If I were doing this problem by hand, how would I do it?
Then, once you thought of a way you would do it, you can write down code to express exactly what you'd do.
I hope this makes sense
~Alex
john larson
16,594 Pointsjohn larson
16,594 Points"If I were doing this problem by hand, how would I do it?" Alex, that is an innovative thought, I'm going to try looking for solutions that way and see what happens...Thanks!
john larson
16,594 Pointsjohn larson
16,594 PointsHi again Alex, I would add that this bit of code "word[:i]" as near as I can tell, on first iteration targets 0 to the index before 0 (which I know doesn't exist, though I would think it wouldn't work, but it does) and that is what seems to make that code work.
Mike Wagner
23,888 PointsMike Wagner
23,888 PointsAlexander Davison - +1 for a solid and clear way of describing it. It's something I know I forget a lot of the time, and it's one of the most helpful things I ever learned as a beginning programmer. When you're first starting out it's really easy to get caught up in the complexity of it and think something is significantly more difficult than it needs to be. The one thing that has helped me the most is taking a more "Ockham's razor" approach to problem solving. Your
falls into line with, this in my opinion, as does
Solid advice.
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsYeah... Actually, when solving problems, there's three key things you should remember: