Archived
1
0

Updating progress on challenge 02. JN now contains functions to perform the algorithm.

This commit is contained in:
Shaun Setlock
2020-08-31 21:43:29 -04:00
parent 236e0a4fc7
commit fc95662a3d
3 changed files with 162 additions and 257 deletions

View File

@@ -1,6 +0,0 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
}

View File

@@ -79,14 +79,38 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def apply_base(n,b):\n",
"def base_ten_to_base_b(n, b):\n",
" '''\n",
" Helper function to convert any base-10 number, n into\n",
" its representation with new base, b.\n",
" '''\n",
" if n == 0:\n",
" return [0]\n",
" rebase = []\n",
" while n:\n",
" rebase.append(int(n % b))\n",
" n //= b\n",
"\n",
" x_list = rebase[::-1]\n",
" x = int(\"\".join(map(str, x_list))) \n",
" \n",
" #take n as a string and convert to list\n",
" n = list(n)\n",
" return x"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def apply_base(n, b):\n",
" \n",
" #take n as a string and convert to list of int's\n",
" n = [int(i) for i in str(n)]\n",
" \n",
" #reverse n so the base loop is more readable\n",
" n.reverse()\n",
@@ -97,269 +121,98 @@
" for i in range(len(n)):\n",
" summation += int(n[i])*b**i\n",
" \n",
" print(summation)\n",
" return summation\n",
" "
" return summation"
]
},
{
"cell_type": "code",
"execution_count": 49,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def number_to_base(n, b):\n",
" if n == 0:\n",
" return [0]\n",
" digits = []\n",
" while n:\n",
" digits.append(int(n % b))\n",
" n //= b\n",
" return digits[::-1]"
"def new_minion_assignment(n, b):\n",
" '''\n",
" Function which applies Commander Bunny's randomization algorithm.\n",
" It takes the existing ID number, n and forms x, y, and computes z.\n",
" DEPENDS: Needs helper function base_ten_to_base_b()\n",
" '''\n",
"\n",
" #print('n = {}'.format(n))\n",
" #print('b = {}'.format(b))\n",
" \n",
" #Define k\n",
" k = len([int(i) for i in str(n)])\n",
" #print('k = {}'.format(k))\n",
" \n",
" #Define x and y\n",
" x_list = sorted([int(i) for i in str(n)],reverse=True)\n",
" y_list = sorted(x_list)\n",
" \n",
" x = int(\"\".join(map(str, x_list)))\n",
" y = int(\"\".join(map(str, y_list)))\n",
" \n",
" #print('x = {}'.format(x))\n",
" #print('y = {}'.format(y))\n",
" \n",
" z = apply_base(x, 3) - apply_base(y, 3)\n",
" \n",
" z = base_ten_to_base_b(z, b)\n",
" \n",
" #Apply padding\n",
" z_list = [int(i) for i in str(z)]\n",
" z_list.reverse()\n",
" while len(z_list) < k:\n",
" z_list.append(0)\n",
" \n",
" z_list.reverse()\n",
" z = \"\".join(map(str, z_list))\n",
"\n",
" print('z = {} ... in base {} ... with padding ... '.format(z, b))\n",
" \n",
" return z"
]
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def find_cycle_length():\n",
" '''\n",
" Function which runs Commander Bunny's randomization algorithm until\n",
" a cycle is detected. The returned value will represent the number of \n",
" minions which are caught inside the reassignment cycle. \n",
" '''\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"575\n"
"z = 02222 ... in base 3 ... with padding ... \n",
"z = 12221 ... in base 3 ... with padding ... \n",
"z = 10212 ... in base 3 ... with padding ... \n",
"z = 20211 ... in base 3 ... with padding ... \n",
"z = 20211 ... in base 3 ... with padding ... \n",
"z = 20211 ... in base 3 ... with padding ... \n",
"z = 20211 ... in base 3 ... with padding ... \n",
"z = 20211 ... in base 3 ... with padding ... \n",
"z = 20211 ... in base 3 ... with padding ... \n",
"z = 20211 ... in base 3 ... with padding ... \n"
]
},
{
"data": {
"text/plain": [
"575"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"apply_base(list('210022'),3)"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"575"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"int('210022',3)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"711\n"
]
},
{
"data": {
"text/plain": [
"711"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"apply_base(list('222100'),3)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"53\n"
]
},
{
"data": {
"text/plain": [
"53"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"apply_base(list('001222'),3)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"711\n",
"53\n"
]
},
{
"data": {
"text/plain": [
"658"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"apply_base(list('222100'),3) - apply_base(list('001222'),3)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 2, 0, 1, 0, 1]"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"number_to_base(int('222100',3) - int('001222',3),3)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 1, 2, 2, 0, 1]"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"number_to_base(int('221100',3) - int('001122',3),3)"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"580\n"
]
},
{
"data": {
"text/plain": [
"580"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"apply_base(list('210111'),3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"210022 -> x = 222100, y = 001222 -> z = x - y = 222100 - 001222 = 210111 ???"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"222100 -> 221400 -> 221330 -> 221323 -> 221253"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"711\n"
]
},
{
"data": {
"text/plain": [
"711"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"apply_base(list('221253'),3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"z = x - y = 221253 - 001222 = 31"
"x = 32333\n",
"for i in range(10):\n",
" x = new_minion_assignment(x, 3)"
]
},
{
@@ -372,21 +225,21 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 2",
"language": "python",
"name": "python3"
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
"pygments_lexer": "ipython2",
"version": "2.7.18"
}
},
"nbformat": 4,

View File

@@ -0,0 +1,58 @@
attrs==20.1.0
backports-abc==0.5
backports.functools-lru-cache==1.6.1
backports.shutil-get-terminal-size==1.0.0
bleach==3.1.5
configparser==4.0.2
contextlib2==0.6.0.post1
decorator==4.4.2
defusedxml==0.6.0
entrypoints==0.3
enum34==1.1.10
functools32==3.2.3.post2
futures==3.3.0
importlib-metadata==1.7.0
ipaddress==1.0.23
ipykernel==4.10.1
ipython==5.10.0
ipython-genutils==0.2.0
ipywidgets==7.5.1
Jinja2==2.11.2
jsonschema==3.2.0
jupyter==1.0.0
jupyter-client==5.3.5
jupyter-console==5.2.0
jupyter-core==4.6.3
MarkupSafe==1.1.1
mistune==0.8.4
nbconvert==5.6.1
nbformat==4.4.0
notebook==5.7.10
packaging==20.4
pandocfilters==1.4.2
pathlib2==2.3.5
pexpect==4.8.0
pickleshare==0.7.5
prometheus-client==0.8.0
prompt-toolkit==1.0.18
ptyprocess==0.6.0
Pygments==2.5.2
pyparsing==2.4.7
pyrsistent==0.16.0
python-dateutil==2.8.1
pyzmq==19.0.2
qtconsole==4.7.6
QtPy==1.9.0
scandir==1.10.0
Send2Trash==1.5.0
simplegeneric==0.8.1
singledispatch==3.4.0.3
six==1.15.0
terminado==0.8.3
testpath==0.4.4
tornado==5.1.1
traitlets==4.3.3
wcwidth==0.2.5
webencodings==0.5.1
widgetsnbextension==3.5.1
zipp==1.2.0