Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Create a function named find_words that takes a count and a string. Return a list of all of the words of length "count"

def find_words(count, st): match = re.findall('\w{count , }' , st) return match

Not sure why it does not work...

def find_words (count, string): return re.findall(r'\w{}'.formart(count),string) this is the code that is working for me but i can see you left out the r and your count should be input therefore you need to use .formart

11 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

MUZ140070 Mupoperi's code does not pass.

You're close, Yaxin Yuan. You can't just stick a variable into a string, though. You need to concatenate the variable into the string.

Chris Wilkes
Chris Wilkes
13,381 Points

I'd just like to say that, after spending the time I did on solving this particular challenge, I think it detracted from the flow of my learning, and while it was certainly tricky, and perhaps good practice for string manipulation, I'm not sure that it was quite as helpful towards the goal learning regex. If the goal was to elucidate the need for concatenation of variables within regex functions, I think a lesser initial challenge might have sufficed. Just saying. ;)

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

The point was to give you a real world example. You'll find yourself needing to match strings of a certain size, or a size-or-smaller, or a size-or-bigger and that's exactly what this challenge is about. It's also tricky and meant to make sure you see how to problem-solve your way through it.

def find_words(my_string, my_count): #put all the words in a list
my_list = list(my_string.split())

#the string that will hold the output my_output_string = []

for each_string in my_list: if len(each_string) == my_count: my_output_string.append(each_string)

return my_output_string

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

That doesn't pass the challenge due to being bad code (for example, you try to split an integer). Also, you shouldn't be using .split(). The purpose of the challenge is to use a regular expression.

Bummer! Didn't get the right output. Output was ['123456,', 'Treehouse,', 'student,', 'learn,', 'Kenneth,', 'Python,', 'regex,', 'match,', 'Ryan,']. Length was 6.

import re def find_words(number, words): return re.findall(r'\w+,', words)

should I be using {}.format and to substitute at this point?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You're just looking for any group of word characters 1 item or longer. You need to control the length, so, yes, you'll have to create the regex string. I don't know that I'd recommend using .format(), though. String concatenation would probably be easier.

import re
def find_words(int, str): 
  return re.findall(r'\w{}'.format(int+str))

... this was close, but not passing. How else can I add them together?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Well, you could add them together.

num = 7
warrior = "Samurai"
title = str(num) + " " + warrior

title would now be "7 Samurai".

Adam Oliver
Adam Oliver
8,214 Points

Can't figure out why it won't take my {count, }?

def find_words(count, string):
    return re.findall(r'\w{count,}', string) 
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Remember, regular expression pattern strings are super-literal, so it's going to look for the letters in count. Or, rather, it'll try to, since that's inside of a count construct so it's probably just getting confused and giving up. You'll need to find a way to use .format() on the string or concatenate what you need.

Adam Oliver
Adam Oliver
8,214 Points

Thanks Kenneth - I'm still gettting an error and I know it will be some small detail I've overlooked.

def find_words(count, string):
  return re.findall(count * r'\w', string)
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Let's say count is 5.

>>> 5 * r'\w'
'\\w\\w\\w\\w\\w'

Is that the string you want? I doubt it.

Also, it's count or more so count * r'\w' won't fit that scenario. You're going to have to use a count { } without an upper limit.

Adam Oliver
Adam Oliver
8,214 Points

It was similarr to the code I posted above, but I gave the count * something to count on from {1,}

Chris Wilkes
Chris Wilkes
13,381 Points

As best as I can tell, the following is what several hinters seem to be eluding to, however it does not appear to solve the challenge, - where am I going wrong here?:

import re def find_words(count, string): count = str(count) return re.findall(r'\w{'+ count + ', }', string)

Adam Oliver
Adam Oliver
8,214 Points

count = str(count) is not needed.

You want to be returning re.findall( inside these you need to be multiplying '\w' against the string) remember to specify 1 or more places use {1, }.

Adam Oliver
Adam Oliver
8,214 Points

I've solved it, but the code feels a little messy. Should I post the solution here for you to comment on? I'd rather not just be handing the answer out.