" words = {w.strip('\"') for w in f.read().split(\",\")}"
]
},
{
"cell_type": "markdown",
"id": "0a73cc47",
"metadata": {},
"source": [
"We'll also write a simple function for testing if a word is a \"triangle word\". We'll make use of SageMath's built-in function for testing if a number is triangular."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "313a760f",
"metadata": {},
"outputs": [],
"source": [
"def is_triangle_word(word):\n",
" value = sum(ord(c) - ord('A') + 1 for c in word)\n",
" return is_triangular_number(value)"
]
},
{
"cell_type": "markdown",
"id": "ba53d2b1",
"metadata": {},
"source": [
"(It's not difficult to apply the quadratic formula to write our own test for [triangular numbers](https://en.wikipedia.org/wiki/Triangular_number).)\n",
"\n",
"Now we just test each word."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "14a9ea9a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"162"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"triangle_words = {w for w in words if is_triangle_word(w)}\n",
"This work is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International license](https://creativecommons.org/licenses/by-sa/4.0/) and the [BSD Zero Clause license](https://spdx.org/licenses/0BSD.html)."