I’ve just merged changes to all the test files for all concept exercises.
Test Error/fail messages should now be more detailed and explicit, but the tests themselves should not have changed…
For an example, you can take a look at the PR for Making the Grade
During the touch-ups, I discovered some errors in Mecha Munch Management, so solutions for that exercise are going to be re-tested.
Hi @BethanyG . I have a doubt in Anagram exercise.
I tested two code versions. The one that is commented did not pass all the tests. But the other that uses list comprehension worked. To me the code does the same, only with a different technique or maybe I am doing something wrong.
Thank you very much in advance. Best Regards
# def find_anagrams(word, candidates):
# anagrams = []
# for item in candidates:
# if sorted(word.lower()) == sorted(item.lower()) and word.lower != item.lower():
# anagrams.append(item)
# return anagrams
def find_anagrams(word, candidates):
return [item for item in candidates if sorted(word.lower()) == sorted(item.lower()) and word.lower() != item.lower()]
Nice question! - and kudos to you for testing out the two methods – because they should be effectively the same (a comprehension is essentially a loop, just inside a literal and with a bit of added “magic”).
Unfortunately, I think you may have made a typo in translating the comprehension to the loop. Note the word.lower without the call (the parenthesis):
def find_anagrams(word, candidates):
anagrams = []
for item in candidates:
if sorted(word.lower()) == sorted(item.lower()) and word.lower != item.lower():
anagrams.append(item)
return anagrams
word.lower would be referring to the lowerattribute or variable of a str object (which doesn’t exist, so Python considers that != as evaluating to True), but what you want here is to call the lower()method of the str class, which will then appropriately evaluate to False in the expression.
So I think if you change word.lower to word.lower() in the loop example, it should pass all the tests exactly the way the comprehension does.
As a future note: requesting mentoring on the website for your solution is an excellent way to get one-on-one questions like this answered, and to work through multiple techniques and solution iterations. This thread is mainly for discussing problems with concept exercise test error messages.