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 trialYaxin Yuan
11,191 PointsCreate 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...
11 Answers
Kenneth Love
Treehouse Guest TeacherMUZ140070 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
13,381 PointsI'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
Treehouse Guest TeacherThe 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.
MUZ140153 Tatenda Zimba
2,782 Pointsdef 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
Treehouse Guest TeacherThat 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.
michaelangelo owildeberry
18,172 PointsBummer! 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
Treehouse Guest TeacherYou'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.
michaelangelo owildeberry
18,172 Pointsimport 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
Treehouse Guest TeacherWell, you could add them together.
num = 7
warrior = "Samurai"
title = str(num) + " " + warrior
title
would now be "7 Samurai"
.
Adam Oliver
8,214 PointsCan't figure out why it won't take my {count, }?
def find_words(count, string):
return re.findall(r'\w{count,}', string)
Kenneth Love
Treehouse Guest TeacherRemember, 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
8,214 PointsThanks 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
Treehouse Guest TeacherLet'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
8,214 PointsIt was similarr to the code I posted above, but I gave the count * something to count on from {1,}
Chris Wilkes
13,381 PointsAs 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)
Kenneth Love
Treehouse Guest Teacher/w
is not \w
.
Adam Oliver
8,214 Pointscount = 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
8,214 PointsI'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.
MUZ140070 Nyasha L. Mupoperi
4,070 PointsMUZ140070 Nyasha L. Mupoperi
4,070 Pointsdef 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