Updating progress on challenge 02. JN now contains functions to perform the algorithm.
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user