Created
May 6, 2016 12:40
-
-
Save Swarchal/449948a35f7d4c0ecdaf1b8fbff9cdd9 to your computer and use it in GitHub Desktop.
partial permutations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Partial Permutations\n", | |
| "\n", | |
| "**Given:** positive integers $n$ and $k$ such that $100 \\geq n > 0$ and $10 \\geq k > 0$\n", | |
| "\n", | |
| "**Return:** the total number of partial permutations $P(n, k)$ mod $1e6$\n", | |
| "\n", | |
| "$$P(n, k) = \\frac{n!}{(n-k)!}$$\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "51200" | |
| ] | |
| }, | |
| "execution_count": 1, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "from math import factorial\n", | |
| "\n", | |
| "def pp(n, k):\n", | |
| " x = factorial(n) / factorial(n-k)\n", | |
| " return int(x % 1e6)\n", | |
| " \n", | |
| "# example data\n", | |
| "pp(21, 7)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Rosalind Data\n", | |
| "and reading from file" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "184000" | |
| ] | |
| }, | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "def partial_permutations(path):\n", | |
| " \n", | |
| " # open file and get values as a list\n", | |
| " nums = map(int, open(path).read().split(\" \"))\n", | |
| " \n", | |
| " # pass first two numbers to `f()`\n", | |
| " return pp(nums[0], nums[1])\n", | |
| "\n", | |
| "my_file = \"/home/scott/Dropbox/rosalind/rosalind_pper.txt\"\n", | |
| "partial_permutations(my_file)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## If you're stuck in the 1980's (or using R)\n", | |
| "\n", | |
| "Have iterate through and modulo as you go to avoid integer overflow" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "184000" | |
| ] | |
| }, | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "n = 83\n", | |
| "k = 9\n", | |
| "\n", | |
| "def part_permute(n, k):\n", | |
| " \n", | |
| " vals = range(n, n-k, -1)\n", | |
| " \n", | |
| " def f(n, k):\n", | |
| " return (n*k) % 1e6\n", | |
| " \n", | |
| " return int(reduce(f, vals))\n", | |
| "\n", | |
| "part_permute(n, k)\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "184000.0" | |
| ] | |
| }, | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "# one liner\n", | |
| "reduce(lambda n, k: (n*k) % 1e6, range(n, n-k, -1))" | |
| ] | |
| } | |
| ], | |
| "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.6" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment