Finds the amount of triangular words in the words file. >>> solution() 162
()
| 21 | |
| 22 | |
| 23 | def solution(): |
| 24 | """ |
| 25 | Finds the amount of triangular words in the words file. |
| 26 | |
| 27 | >>> solution() |
| 28 | 162 |
| 29 | """ |
| 30 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 31 | words_file_path = os.path.join(script_dir, "words.txt") |
| 32 | |
| 33 | words = "" |
| 34 | with open(words_file_path) as f: |
| 35 | words = f.readline() |
| 36 | |
| 37 | words = [word.strip('"') for word in words.strip("\r\n").split(",")] |
| 38 | words = [ |
| 39 | word |
| 40 | for word in [sum(ord(x) - 64 for x in word) for word in words] |
| 41 | if word in TRIANGULAR_NUMBERS |
| 42 | ] |
| 43 | return len(words) |
| 44 | |
| 45 | |
| 46 | if __name__ == "__main__": |