Archived
1
0

Pushing local repo up to github.

This commit is contained in:
Shaun Setlock
2020-08-30 22:07:31 -04:00
commit cab255efe9
3 changed files with 529 additions and 0 deletions

129
.gitignore vendored Normal file
View File

@@ -0,0 +1,129 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
.python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/

View File

@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
}

View File

@@ -0,0 +1,394 @@
{
"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": 31,
"metadata": {},
"outputs": [],
"source": [
"def apply_base(n,b):\n",
" \n",
" #take n as a string and convert to list\n",
" n = list(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",
" print(summation)\n",
" return summation\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 49,
"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]"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"575\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"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}