finding anagrams of a string in python -


i wrote code anagrams not giving result test cases

import itertools def find_all_anagrams(words, word):         if word none:             return []         else:             m = set()             permutation in itertools.permutations(word.lower()):                 m.add("".join(permutation))             ls = []             anagram in m:                 ls.append(anagram)             n = [i in words if in ls]             return n         pass  assert ["not", "ton"] == find_all_anagrams(["ant", "not", "ton"], "ont") assert ["ant", "tan"] == find_all_anagrams(["ant", "not", "ton", "tan"], "tan") assert [] == find_all_anagrams(["ant", "not", "ton", "tan"], "abc") assert [] == find_all_anagrams(["ant", "not", "ton", "tan"], none) assert [] == find_all_anagrams(none, none) assert ["ant", "tan"] == find_all_anagrams(["ant", "not", "ton", "tan"], "tan") 

but test case giving empty list

assert ["ant", "tan"] == find_all_anagrams(["ant", "not", "ton", "tan"], "tan") 

check code

this letter case problem; if make words lower-case, works:

>>> find_all_anagrams(["ant", "not", "ton", "tan"], "tan") ['ant', 'tan'] 

you can minimally modify function follows:

return [i in words if i.lower() in ls] 

(note no need assign n). gives output expect:

>>> find_all_anagrams(["ant", "not", "ton", "tan"], "tan") ['ant', 'tan'] 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -