Skip to content

Instantly share code, notes, and snippets.

@vaibhavsagar
Created September 21, 2016 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaibhavsagar/3849363bcbbefe83863ae75afd272a65 to your computer and use it in GitHub Desktop.
Save vaibhavsagar/3849363bcbbefe83863ae75afd272a65 to your computer and use it in GitHub Desktop.
2016-09-20 solution with @satabdidas
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'2783915460'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import itertools\n",
"i = 0\n",
"def counter():\n",
" global i\n",
" i += 1\n",
" return i\n",
"\n",
"# print(list(itertools.permutations('0123', 4)))\n",
"''.join(next(itertools.dropwhile(lambda _: counter() < 1000000, itertools.permutations('0123456789', 10))))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ls ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']\n",
"remainder 1000000\n",
"index 2\n",
"ls ['0', '1', '3', '4', '5', '6', '7', '8', '9']\n",
"remainder 274240\n",
"index 6\n",
"ls ['0', '1', '3', '4', '5', '6', '8', '9']\n",
"remainder 32320\n",
"index 6\n",
"ls ['0', '1', '3', '4', '5', '6', '9']\n",
"remainder 2080\n",
"index 2\n",
"ls ['0', '1', '4', '5', '6', '9']\n",
"remainder 640\n",
"index 5\n",
"ls ['0', '1', '4', '5', '6']\n",
"remainder 40\n",
"index 1\n",
"ls ['0', '4', '5', '6']\n",
"remainder 16\n",
"index 2\n",
"ls ['0', '4', '6']\n",
"remainder 4\n",
"index 2\n",
"ls ['0', '4']\n",
"remainder 0\n",
"index 0\n"
]
},
{
"data": {
"text/plain": [
"'278391560'"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def fact(n):\n",
" if n == 1:\n",
" return 1\n",
" return n * fact(n - 1)\n",
"\n",
"def permutation_at(number):\n",
" ls = list('0123456789')\n",
" output = []\n",
" remainder = number\n",
" n = 9\n",
" while n > 0:\n",
" index = remainder // fact(n)\n",
" print('ls', ls)\n",
" print('remainder', remainder)\n",
" print('index', index)\n",
" \n",
" remainder = remainder % fact(n)\n",
" n -= 1\n",
" output.append(ls[index])\n",
" del ls[index]\n",
" return ''.join(output)\n",
"permutation_at(1000000) "
]
}
],
"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.4.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment