Add files via upload
This commit is contained in:
@@ -26,7 +26,7 @@
|
||||
"source": [
|
||||
"# Making Bracelets\n",
|
||||
"\n",
|
||||
"The function `bracelet` will make a number bracelet, if you give is a list of two starting beads:"
|
||||
"The function `bracelet` will make a number bracelet, if you give is a pair of starting beads:"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -39,8 +39,8 @@
|
||||
"def bracelet(beads):\n",
|
||||
" \"\"\"Given a list of two beads, extend it to be a bracelet, following the rules of the game.\"\"\"\n",
|
||||
" while len(beads) < 4 or beads[:2] != beads[-2:]:\n",
|
||||
" new_bead = (beads[-1] + beads[-2]) % 10\n",
|
||||
" beads = beads + [new_bead]\n",
|
||||
" next_bead = (beads[-1] + beads[-2]) % 10\n",
|
||||
" beads = (*beads, next_bead)\n",
|
||||
" return beads[:-2]"
|
||||
]
|
||||
},
|
||||
@@ -61,7 +61,7 @@
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[2, 6, 8, 4]"
|
||||
"(2, 6, 8, 4)"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
@@ -70,7 +70,7 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"bracelet([2, 6])"
|
||||
"bracelet((2, 6))"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -82,7 +82,7 @@
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[1, 3, 4, 7, 1, 8, 9, 7, 6, 3, 9, 2]"
|
||||
"(1, 3, 4, 7, 1, 8, 9, 7, 6, 3, 9, 2)"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
@@ -91,7 +91,7 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"bracelet([1, 3])"
|
||||
"bracelet((1, 3))"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -111,7 +111,7 @@
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[0, 0]"
|
||||
"(0, 0)"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
@@ -120,7 +120,7 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"bracelet([0, 0])"
|
||||
"bracelet((0, 0))"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -249,10 +249,68 @@
|
||||
"source": [
|
||||
"digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
|
||||
"\n",
|
||||
"for first in digits:\n",
|
||||
" for second in digits:\n",
|
||||
" beads = bracelet([first, second])\n",
|
||||
" print(f'{len(beads):2} beads:', *beads)"
|
||||
"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",
|
||||
"\n",
|
||||
"all_bracelets = [bracelet((first, second)) for first in digits for second in digits]\n",
|
||||
"\n",
|
||||
"show(all_bracelets)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "21dc3fe1-dc06-4b57-a7b8-e21502086ac9",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# How Many Bracelets?\n",
|
||||
"\n",
|
||||
"I showed 100 bracelets, because there are 100 two-digit starting pairs. But consider thesde three results:\n",
|
||||
"- 0 5 5\n",
|
||||
"- 5 0 5\n",
|
||||
"- 5 5 0\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:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "9c6ed448-87a2-42f4-ab7b-75758e734cd2",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" 2 beads: 0 0\n",
|
||||
" 4 beads: 2 6 8 4\n",
|
||||
" 3 beads: 0 5 5\n",
|
||||
"12 beads: 1 3 4 7 1 8 9 7 6 3 9 2\n",
|
||||
"60 beads: 0 1 1 2 3 5 8 3 1 4 5 9 4 3 7 0 7 7 4 1 5 6 1 7 8 5 3 8 1 9 0 9 9 8 7 5 2 7 9 6 5 1 6 7 3 0 3 3 6 9 5 4 9 3 2 5 7 2 9 1\n",
|
||||
"20 beads: 0 2 2 4 6 0 6 6 2 8 0 8 8 6 4 0 4 4 8 2\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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})"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "5276c097-6a6b-4b6a-b146-ef2fae4a8ecd",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"We see there are only six distinct bracelets."
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user