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

Python

I'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

For 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 :grin:

:dizzy: ~Alex :dizzy:

"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!

Hi 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
Mike Wagner
23,888 Points

Alexander 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

If I were doing this problem by hand, how would I do it?

falls into line with, this in my opinion, as does

How can I solve this in the simplest way possible?

Solid advice.

Yeah... Actually, when solving problems, there's three key things you should remember:

  • If I were doing this problem by hand, how would I do it?
  • Is it easy to understand and read? (Pro tip: Use helpful comments!)
  • Is it fast for the computer to run? For example, Merge Sort is a better sorting algorithm then Monkey sort. (Merge Sort is the fastest algorithm for sorting a list that I know while Monkey Sort is the worst)