from itertools import combinations from collections import Counter with open('data04.txt') as f: phrases = [r.strip() for r in f.readlines()] def test_words(phrase): words = phrase.split() unique = set(words) return len(unique) == len(words) def test_anagrams(phrase): words = phrase.split() for a, b in combinations(words, 2): if Counter(a) == Counter(b): return False return True total = 0 ''' # part 1 for phrase in phrases: if test_words(phrase): total += 1 ''' # part 2 for phrase in phrases: if test_anagrams(phrase): total += 1 print(total)