Created
November 7, 2015 14:52
-
-
Save Jonarzz/802531528d562d01bc8f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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