Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import unittest
def find_anagrams(list_of_strings, word):
anagrams = []
for str_element in list_of_strings:
if len(word) == len(str_element):
list_of_chars = list(str_element)
for char in word:
if char in list_of_chars:
list_of_chars.remove(char)
if len(list_of_chars) == 0:
anagrams.append(str_element)
return anagrams
class AnagramsTestCase(unittest.TestCase):
def test_parse(self):
result_list = find_anagrams(['spare', 'hello', 'pears', 'world', 'reaps'], 'parse')
correct_list = ['spare', 'pears', 'reaps']
self.assertEqual(result_list, correct_list)
def test_python(self):
result_list = find_anagrams(['asdasdasd', 'asdasd', 'ytphno', 'pythno', 'pythom', 'nohtyp', 'pppppp', 'python'], 'python')
correct_list = ['ytphno', 'pythno', 'nohtyp', 'python']
self.assertEqual(result_list, correct_list)
def test_scholarship(self):
result_list = find_anagrams(['scholsrahip', 'scholarshir', 'hscolaphisr', 'solarship', 'szholarship', 'hsclaophisr'], 'scholarship')
correct_list = ['scholsrahip', 'hscolaphisr', 'hsclaophisr']
self.assertEqual(result_list, correct_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.