{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Hey, I Already Did That!\n", "========================\n", "\n", "Commander Lambda uses an automated algorithm to assign minions randomly to tasks, in order to keep her minions on their toes. But you've noticed a flaw in the algorithm - it eventually \n", "loops back on itself, so that instead of assigning new minions as it iterates, it gets stuck in a cycle of values so that the same minions end up doing the same tasks over and over again. You \n", "think proving this to Commander Lambda will help you make a case for your next promotion. \n", "\n", "You have worked out that the algorithm has the following process: \n", "\n", "1) Start with a random minion ID n, which is a nonnegative integer of length k in base b\n", "2) Define x and y as integers of length k. x has the digits of n in descending order, and y has the digits of n in ascending order\n", "3) Define z = x - y. Add leading zeros to z to maintain length k if necessary\n", "4) Assign n = z to get the next minion ID, and go back to step 2\n", "\n", "For example, given minion ID n = 1211, k = 4, b = 10, then x = 2111, y = 1112 and z = 2111 - 1112 = 0999. Then the next minion ID will be n = 0999 and the algorithm iterates again: x = 9990, \n", "y = 0999 and z = 9990 - 0999 = 8991, and so on.\n", "\n", "Depending on the values of n, k (derived from n), and b, at some point the algorithm reaches a cycle, such as by reaching a constant value. For example, starting with n = 210022, k = 6, b = \n", "3, the algorithm will reach the cycle of values [210111, 122221, 102212] and it will stay in this cycle no matter how many times it continues iterating. Starting with n = 1211, the routine \n", "will reach the integer 6174, and since 7641 - 1467 is 6174, it will stay as that value no matter how many times it iterates.\n", "\n", "Given a minion ID as a string n representing a nonnegative integer of length k in base b, where 2 <= k <= 9 and 2 <= b <= 10, write a function solution(n, b) which returns the \n", "length of the ending cycle of the algorithm above starting with n. For instance, in the example above, solution(210022, 3) would return 3, since iterating on 102212 would return to 210111 \n", "when done in base 3. If the algorithm reaches a constant, such as 0, then the length is 1.\n", "\n", "Languages\n", "=========\n", "\n", "To provide a Java solution, edit Solution.java\n", "To provide a Python solution, edit solution.py\n", "\n", "Test cases\n", "==========\n", "Your code should pass the following test cases.\n", "Note that it may also be run against hidden test cases not shown here.\n", "\n", "-- Python cases -- \n", "Input:\n", "solution.solution('1211', 10)\n", "Output:\n", " 1\n", "\n", "Input:\n", "solution.solution('210022', 3)\n", "Output:\n", " 3\n", "\n", "Use verify [file] to test your solution and see how it does. When you are finished editing your code, use submit [file] to submit your answer. If your solution passes the test cases, it will \n", "be removed from your home folder." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Some brushing up on \"base\" ...\n", "\n", "Recall that to compute an integer i from a number n in base b, apply:\n", "\n", "`i = (n[0] * b**0) + (n[1] * b**1) + ... `\n", "\n", "For example, what is i if b = 2 and n = 1010?\n", "\n", "`i = (0 * 1) + (1 * 2) + (0 * 4) + (1 * 8) = 0 + 2 + 0 + 8 = 10` " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "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", " 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", " \n", " #initialize the summation\n", " summation = 0\n", " \n", " for i in range(len(n)):\n", " summation += int(n[i])*b**i\n", " \n", " return summation" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "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", " #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" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "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)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[111, 111]\n" ] }, { "data": { "text/plain": [ "1" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "solution(111,2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.18" } }, "nbformat": 4, "nbformat_minor": 4 }