Add files via upload

This commit is contained in:
Peter Norvig
2024-05-01 12:57:49 -07:00
committed by GitHub
parent d056324c70
commit 819adb60f8

View File

@@ -252,7 +252,7 @@
"def show(bracelets):\n",
" \"\"\"Print each of the bracelets, preceeded by its number of beads.\"\"\"\n",
" for beads in bracelets:\n",
" print(f'{len(beads):2} beads:', *beads)\n",
" print(f'{len(beads):2} beads:', ''.join(map(str, beads)))\n",
"\n",
"all_bracelets = [bracelet((first, second)) for first in digits for second in digits]\n",
"\n",
@@ -266,18 +266,47 @@
"source": [
"# How Many Bracelets?\n",
"\n",
"I showed 100 bracelets, because there are 100 two-digit starting pairs. But consider thesde three results:\n",
"I showed 100 bracelets, because there are 100 two-digit starting pairs. But consider these three results:\n",
"- 055\n",
"- 505\n",
"- 550\n",
"\n",
"These are all different as *sequences*, but they are all the same as *circular bracelets*: two 5s and a 0. So let's see how many different bracelets there are. I'll do that by converting all three of these into one common form. I'll arbitrarily choose the form that would be the lowest number: 055 is lower than 505 or 550. Then I'll make a set of all the distinct common forms and show them:"
"These are all different as *sequences*, but they are all the same as *circular bracelets*: if you start at the 0 and go around the circle they all are equal to \"055.\" So let's see how many *different* bracelets there are. I'll do that by converting all bracelets into one common form. Out of all the possible starting points on the circle, I'll arbitrarily choose the starting point that would yield the lowest number: 055 is lower than 505 or 550, so 055 will be the common form. Then I'll make a set of all the distinct common forms and show them:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "9c6ed448-87a2-42f4-ab7b-75758e734cd2",
"id": "9d1b4cca-cc8c-4268-9c5b-b9d2f2909948",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(0, 5, 5)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def common_form(bracelet):\n",
" \"\"\"Represent this bracelet by choosing the lowest numerical rotation out of all possible rotations.\"\"\"\n",
" return min(rotations(bracelet))\n",
"\n",
"def rotations(bracelet):\n",
" \"\"\"All possible rotations of bracelet.\"\"\"\n",
" return [bracelet[i:] + bracelet[:i] for i in range(len(bracelet))]\n",
"\n",
"common_form((5, 0, 5))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "9ae033fd-062e-4953-9603-4f82da516057",
"metadata": {},
"outputs": [
{
@@ -294,14 +323,6 @@
}
],
"source": [
"def common_form(bracelet):\n",
" \"\"\"Represent this bracelet by choosing the lowest numerical rotation out of all possible rotations.\"\"\"\n",
" return min(rotations(bracelet))\n",
"\n",
"def rotations(bracelet):\n",
" \"\"\"All possible rotations of bracelet.\"\"\"\n",
" return [bracelet[i:] + bracelet[:i] for i in range(len(bracelet))]\n",
"\n",
"show({common_form(beads) for beads in all_bracelets})"
]
},