Pushing up completed material from challenge 02!
This commit is contained in:
BIN
challenge_02/Screenshot from 2020-09-01 21-15-22.png
Normal file
BIN
challenge_02/Screenshot from 2020-09-01 21-15-22.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 162 KiB |
@@ -136,57 +136,138 @@
|
|||||||
" It takes the existing ID number, n and forms x, y, and computes z.\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",
|
" DEPENDS: Needs helper function base_ten_to_base_b()\n",
|
||||||
" '''\n",
|
" '''\n",
|
||||||
"\n",
|
|
||||||
" #print('n = {}'.format(n))\n",
|
|
||||||
" #print('b = {}'.format(b))\n",
|
|
||||||
" \n",
|
|
||||||
" #Define k\n",
|
" #Define k\n",
|
||||||
" k = len([int(i) for i in str(n)])\n",
|
" k = len([int(i) for i in str(n)])\n",
|
||||||
" #print('k = {}'.format(k))\n",
|
|
||||||
" \n",
|
|
||||||
" #Define x and y\n",
|
" #Define x and y\n",
|
||||||
" x_list = sorted([int(i) for i in str(n)],reverse=True)\n",
|
" x_list = sorted([int(i) for i in str(n)],reverse=True)\n",
|
||||||
" y_list = sorted(x_list)\n",
|
" y_list = sorted(x_list)\n",
|
||||||
" \n",
|
|
||||||
" x = int(\"\".join(map(str, x_list)))\n",
|
" x = int(\"\".join(map(str, x_list)))\n",
|
||||||
" y = int(\"\".join(map(str, y_list)))\n",
|
" y = int(\"\".join(map(str, y_list)))\n",
|
||||||
" \n",
|
" #Calculate z\n",
|
||||||
" #print('x = {}'.format(x))\n",
|
" z = apply_base(x, b) - apply_base(y, b)\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",
|
" z = base_ten_to_base_b(z, b)\n",
|
||||||
" \n",
|
|
||||||
" #Apply padding\n",
|
" #Apply padding\n",
|
||||||
" z_list = [int(i) for i in str(z)]\n",
|
" z_list = [int(i) for i in str(z)]\n",
|
||||||
" z_list.reverse()\n",
|
" z_list.reverse()\n",
|
||||||
" while len(z_list) < k:\n",
|
" while len(z_list) < k:\n",
|
||||||
" z_list.append(0)\n",
|
" z_list.append(0)\n",
|
||||||
" \n",
|
|
||||||
" z_list.reverse()\n",
|
" z_list.reverse()\n",
|
||||||
" z = \"\".join(map(str, z_list))\n",
|
" z = \"\".join(map(str, z_list))\n",
|
||||||
"\n",
|
|
||||||
" print('z = {} ... in base {} ... with padding ... '.format(z, b))\n",
|
|
||||||
" \n",
|
|
||||||
" return z"
|
" return z"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"---"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 17,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def find_cycle_length(s):\n",
|
"def solution(n, b):\n",
|
||||||
" '''\n",
|
" \n",
|
||||||
" Function which runs Commander Bunny's randomization algorithm until\n",
|
" #Start by defining some function which we will reuse.\n",
|
||||||
" a cycle is detected. The returned value will represent the number of \n",
|
" \n",
|
||||||
" minions which are caught inside the reassignment cycle. \n",
|
" def base_ten_to_base_b(n, b):\n",
|
||||||
" '''\n",
|
" '''\n",
|
||||||
" i = (s+s).find(s, 1, -1)\n",
|
" Helper function to convert any base-10 number, n into\n",
|
||||||
" print(i)\n",
|
" its representation with new base, b.\n",
|
||||||
" return None if i == -1 else s[:i]"
|
" '''\n",
|
||||||
|
" # The int() function does this in python3, but I can't get it\n",
|
||||||
|
" # to work in python 2.7.13 ...\n",
|
||||||
|
" if n == 0:\n",
|
||||||
|
" return [0]\n",
|
||||||
|
" rebase = []\n",
|
||||||
|
" while n:\n",
|
||||||
|
" rebase.append(int(n % b))\n",
|
||||||
|
" n //= b\n",
|
||||||
|
" x_list = rebase[::-1]\n",
|
||||||
|
" x = int(\"\".join(map(str, x_list))) \n",
|
||||||
|
" return x\n",
|
||||||
|
" \n",
|
||||||
|
" def apply_base(n, b):\n",
|
||||||
|
" '''\n",
|
||||||
|
" Helper function to convert any number of base b into its\n",
|
||||||
|
" base ten representation. \n",
|
||||||
|
" '''\n",
|
||||||
|
" # I know this isn't really necessary,\n",
|
||||||
|
" # and I could just do the subraction in the provided base.\n",
|
||||||
|
" n = [int(i) for i in str(n)]\n",
|
||||||
|
" n.reverse()\n",
|
||||||
|
" summation = 0\n",
|
||||||
|
" for i in range(len(n)):\n",
|
||||||
|
" summation += int(n[i])*b**i\n",
|
||||||
|
" return summation\n",
|
||||||
|
" \n",
|
||||||
|
" 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",
|
||||||
|
" #Define k\n",
|
||||||
|
" k = len([int(i) for i in str(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",
|
||||||
|
" x = int(\"\".join(map(str, x_list)))\n",
|
||||||
|
" y = int(\"\".join(map(str, y_list)))\n",
|
||||||
|
" if x == y:\n",
|
||||||
|
" return n\n",
|
||||||
|
" #Calculate z\n",
|
||||||
|
" z = apply_base(x, b) - apply_base(y, b)\n",
|
||||||
|
" z = base_ten_to_base_b(z, b)\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",
|
||||||
|
" z_list.reverse()\n",
|
||||||
|
" z = \"\".join(map(str, z_list))\n",
|
||||||
|
" return z\n",
|
||||||
|
" \n",
|
||||||
|
" # Function start. \n",
|
||||||
|
" not_match = True\n",
|
||||||
|
" sequence_list = []\n",
|
||||||
|
" while not_match:\n",
|
||||||
|
" n = new_minion_assignment(n, b)\n",
|
||||||
|
" sequence_list.append(n)\n",
|
||||||
|
" if n in sequence_list[:-1]:\n",
|
||||||
|
" not_match = False\n",
|
||||||
|
" print(sequence_list)\n",
|
||||||
|
" return len(sequence_list)-1-sequence_list.index(n)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 18,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"['65199744', '85308642', '86308632', '86326632', '64326654', '43208766', '85317642', '75308643', '84308652', '86308632']\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"7"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 18,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"solution(12112376,10)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -198,36 +279,22 @@
|
|||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"z = 220101 ... in base 3 ... with padding ... \n",
|
"[111, 111]\n"
|
||||||
"z = 212201 ... in base 3 ... with padding ... \n",
|
|
||||||
"z = 210111 ... in base 3 ... with padding ... \n",
|
|
||||||
"z = 122221 ... in base 3 ... with padding ... \n",
|
|
||||||
"z = 102212 ... in base 3 ... with padding ... \n",
|
|
||||||
"z = 210111 ... in base 3 ... with padding ... \n",
|
|
||||||
"['220101', '212201', '210111', '122221', '102212']\n",
|
|
||||||
"5\n",
|
|
||||||
"2\n",
|
|
||||||
"102212\n"
|
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"1"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 19,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"not_match = True\n",
|
"solution(111,2)"
|
||||||
"sequence_list = []\n",
|
|
||||||
"x = 210022\n",
|
|
||||||
"i = 0\n",
|
|
||||||
"while i<10 and not_match:\n",
|
|
||||||
" x = new_minion_assignment(x, 3)\n",
|
|
||||||
" if x in sequence_list:\n",
|
|
||||||
" pass\n",
|
|
||||||
" not_match = False\n",
|
|
||||||
" else:\n",
|
|
||||||
" sequence_list.append(x)\n",
|
|
||||||
" i += 1\n",
|
|
||||||
"print(sequence_list)\n",
|
|
||||||
"print(i-1)\n",
|
|
||||||
"print(sequence_list.index(x))\n",
|
|
||||||
"print(sequence_list[i-2])"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -235,27 +302,7 @@
|
|||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": []
|
||||||
"sequence = \"\"\n",
|
|
||||||
"sequence_list = []\n",
|
|
||||||
"x = 32333\n",
|
|
||||||
"for i in range(10):\n",
|
|
||||||
" x = new_minion_assignment(x, 3)\n",
|
|
||||||
" sequence += x\n",
|
|
||||||
" sequence_list.append(x)\n",
|
|
||||||
" print(sequence)\n",
|
|
||||||
" print(sequence_list)\n",
|
|
||||||
" "
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"find_cycle_length('12341234')"
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
|||||||
Reference in New Issue
Block a user