Archived
1
0
This repository has been archived on 2025-04-27. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
google-foobar/challenge_02/challenge_02.ipynb

290 lines
9.0 KiB
Plaintext

{
"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",
"\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": 4,
"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]"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"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"
]
}
],
"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])"
]
},
{
"cell_type": "code",
"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')"
]
},
{
"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
}