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 trialKevin Lozandier
Courses Plus Student 53,747 PointsPython regular expression count challenge seemingly incorrectly recognizes an incomplete answer to me as being correct?
In the Python RegEx course, a challenge that wants challenge takers to create a function that takes an integer and return words that are as long or longer as the passed in integer seems to accept an incomplete answer:
Ideally--assuming the parameters are n, and s-- the RegEx would be the following
r'\w{n, }'
However, the challenge takers are not yet taught (frankly I don't even know it's possible) how to pass in a variable value inside a regular expression; the format()
method (and its shortcut) of course is invalid.
Instead I attempted a faux answer that doesn't meet the requirements of the function asked.
I used rs = "\w * n" and
r'+'(the latter of course changes the entire string to a raw string) within
re.findall` and the challenge marked it as correct.
This is seemingly an error unless I'm mistaken to believe an implicit group was made by regular expression; I continuously attempt to frame it as expressing "find a string with n
amount of characters one or more times instead of having at least n
characters.
I may perhaps be more rusty with Regular Expressions more than I thought (or picked a horrible evening to understand them within Python, hehe).
Perhaps, Kenneth Love can provide an explanation?
import re
# EXAMPLE:
# >>> find_words(4, "dog, cat, baby, balloon, me")
# ['baby', 'balloon']
def find_words(n, s):
rs = "\w" * n
return re.findall(rs+r'+', s)
import re
def find_words(n, s):
rs = "\w" * n
return re.findall(rs+r'+', s)
1 Answer
Kenneth Love
Treehouse Guest Teacherr'\w+'
and r'\w{5,}'
are, of course, not the same thing. r'\w\w\w\w\w+'
and r'\w{5,}'
, though, would be (find 4 word characters, then at least 1 more word character). Either one of these should be acceptable for the challenge.
As for using .format()
, no, there is not an easy way to use it. %s
style substitution should still work, as will string concatenation.
Kevin Lozandier
Courses Plus Student 53,747 PointsKevin Lozandier
Courses Plus Student 53,747 PointsThanks for clarifying that, Kenneth!