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 trialsulaiman gannas
Python Web Development Techdegree Student 17,651 Pointscan't solve this problem
Please assist
import unittest
from string_fun import get_anagrams
class AnagramTestCase(unittest.TestCase):
def test_empty_string(self):
with self.assertRaises(ValueError):
get_anagrams('')
import itertools
def is_palindrome(yarn):
"""Return whether or not a string is a palindrome.
A palindrome is a word/phrase that's the same in
both directions.
"""
return yarn == yarn[::-1]
def get_anagrams(*yarn):
"""Return a list of anagrams for a string."""
# If only one letter came in, return it
if yarn:
if len(yarn[0]) <= 1:
return list(yarn)
else:
raise ValueError("Must provide at least two letters")
# Get all of the words from the dictionary
words = set([
w.strip().lower() for w in open('words.txt')
])
output = set()
for thread in yarn:
thread = thread.lower()
# Find all possible anagrams
for i in range(2, len(thread)):
fibers = set(
[''.join(w) for w in itertools.permutations(thread, i)]
)
output.update(fibers.intersection(words))
# Finally, return all of the combinations that are in the dictionary
return sorted(list(output))
Chris Freeman
Treehouse Moderator 68,441 Pointssulaiman gannas: Your code appears to pass the first task in assertRaises. What is the question regarding the second task?
Challenge Task 2 of 2
Now add a new test, test_no_args
that should also assertRaises(ValueError)
. This time, call get_anagrams()
with no arguments.
Basically, you need to add a second function with this new name, change the arguments used when calling get_anagrams()
.
2 Answers
Jeffrey McGowan
4,250 Pointsthe first task worked with:
class AnagramTestCase(unittest.TestCase):
def test_empty_string(self):
with self.assertRaises(ValueError):
get_anagrams('')
while the second worked with:
class AnagramTestCase(unittest.TestCase):
def test_empty_string(self):
with self.assertRaises(ValueError):
get_anagrams()
note that there's literally nothing in the parentheses there. i have the unfortunate habit of putting spaces ( ) in there which the grader didn't like : )
[MOD: added ```python markdown formatting -cf]
Chris Freeman
Treehouse Moderator 68,441 PointsJeffrey, You are correct. I confirmed the assertRaises challenge will reject the second test if a space is between the parens in get_anagrams( )
. This is in keeping with PEP8 coding guidelines, but perhaps is too harsh for the challenge. Tagging Kenneth Love for possible grader tweak to allow the whitespace.
Kenneth Love
Treehouse Guest TeacherUpdated the validator.
Like Chris said, it's a good idea to abide by PEP8.
Jeremy Fisk
7,768 PointsI had the same code for the first task except the last line I had double quotations for the empty string like this:
get_anagrams("")
and my code did not pass until I used single quotations., does anyone know why this happened?
Chris Freeman
Treehouse Moderator 68,441 PointsCode challenge graders get updated if issues have been found. This must have happen, I have verified that either single- or double-quotes will pass the first task.
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsSide issue for Kenneth Love: The embedded links in the code headers for
tests.py
andstring_fun.py
cannot differentiate between the two challenges: assertIn and assertRaises. both are using the <code>``tests.py</code> naming. This post is about
assertRaisesbut the links redirect to
assertIn`.