diff --git a/challenge_02/Screenshot from 2020-09-01 21-15-22.png b/challenge_02/Screenshot from 2020-09-01 21-15-22.png new file mode 100644 index 0000000..c76153f Binary files /dev/null and b/challenge_02/Screenshot from 2020-09-01 21-15-22.png differ diff --git a/challenge_02/challenge_02.ipynb b/challenge_02/challenge_02.ipynb index 3313780..d9d30d9 100644 --- a/challenge_02/challenge_02.ipynb +++ b/challenge_02/challenge_02.ipynb @@ -136,57 +136,138 @@ " 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", + " #Calculate z\n", + " z = apply_base(x, b) - apply_base(y, b)\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": "markdown", + "metadata": {}, + "source": [ + "---" + ] + }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ - "def find_cycle_length(s):\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", - " i = (s+s).find(s, 1, -1)\n", - " print(i)\n", - " return None if i == -1 else s[:i]" + "def solution(n, b):\n", + " \n", + " #Start by defining some function which we will reuse.\n", + " \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", + " # 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", "output_type": "stream", "text": [ - "z = 220101 ... in base 3 ... with padding ... \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" + "[111, 111]\n" ] + }, + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "not_match = True\n", - "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])" + "solution(111,2)" ] }, { @@ -235,27 +302,7 @@ "execution_count": null, "metadata": {}, "outputs": [], - "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')" - ] + "source": [] }, { "cell_type": "code",