Computer science project

profileJerry Shen
Mod_2_Day_2_Filledin.ipynb

{ "cells": [ { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import random\n", "import numpy" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#Function to turn monetary values into numeric quantities\n", "#def name_you_pick(inputs/arguments):\n", "def convert(amount):\n", " result = float(amount[1:])\n", " return result" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "56.42 is r1\n" ] }, { "ename": "NameError", "evalue": "name 'amount' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-5-ce0053f1a69d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mr1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mconvert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m#call the function function_name(inputs/arguments)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mr1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'is r1'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mamount\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'amount' is not defined" ] } ], "source": [ "#Main program\n", "b = '$56.42'\n", "r1 = convert(b) #call the function function_name(inputs/arguments)\n", "print(r1,'is r1')\n", "print(amount)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#Same as above, but with a list, remove your lowest score\n", "def drop_grade(grades):\n", " result = sorted(grades)[1:]\n", " #print('Just checking', result)\n", " return result\n", " " ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Just checking [87, 90, 95, 100]\n", "[87, 90, 95, 100]\n", "93.0\n" ] } ], "source": [ "scores = [90, 87, 95, 67, 100]\n", "final = drop_grade(scores)\n", "print(final)\n", "print(numpy.mean(final)) #module_name.function_name(inputs/arguments)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def drop_grade(grades, num_to_drop):\n", " result = sorted(grades)[num_to_drop:]\n", " return result" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[87, 90, 95, 95, 100]\n", "93.4\n" ] } ], "source": [ "scores = [90, 87, 95, 67, 100, 72, 95]\n", "final = drop_grade(scores, 2)\n", "print(final)\n", "print(numpy.mean(final)) #Is this right?" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#optional arguments...assigned a value, opt_arg = something\n", "def drop_grade(grades, num_to_drop = 1):\n", " result = sorted(grades)[num_to_drop:]\n", " return result" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[87, 90, 95, 95, 100]\n", "93.4\n" ] } ], "source": [ "scores = [90, 87, 95, 67, 100, 72, 95]\n", "final = drop_grade(scores, 2)\n", "print(final)\n", "print(numpy.mean(final)) #Is this right?" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[72, 87, 90, 95, 95, 100]\n", "89.8333333333\n" ] } ], "source": [ "scores = [90, 87, 95, 67, 100, 72, 95]\n", "final = drop_grade(scores) #What will happen here?\n", "print(final)\n", "print(numpy.mean(final)) #Is this right?" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def get_random_students(students_list, num = 4):\n", " return random.sample(students_list, num)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Banana', 'Pineapple', 'Tortoise', 'Cherries']\n" ] } ], "source": [ "students = ['Apple', 'Banana', 'Cherries', 'Pineapple', 'Turtle', 'Kiwi', 'Tortoise']\n", "result = get_random_students(students)\n", "print(result)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on method sample in module random:\n", "\n", "sample(population, k) method of random.Random instance\n", " Chooses k unique random elements from a population sequence or set.\n", " \n", " Returns a new list containing elements from the population while\n", " leaving the original population unchanged. The resulting list is\n", " in selection order so that all sub-slices will also be valid random\n", " samples. This allows raffle winners (the sample) to be partitioned\n", " into grand prize and second place winners (the subslices).\n", " \n", " Members of the population need not be hashable or unique. If the\n", " population contains repeats, then each occurrence is a possible\n", " selection in the sample.\n", " \n", " To choose a sample in a range of integers, use range as an argument.\n", " This is especially fast and space efficient for sampling from a\n", " large population: sample(range(10000000), 60)\n", "\n" ] } ], "source": [ "help(random.sample)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "ename": "ValueError", "evalue": "Sample larger than population or is negative", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-28-55a4edf5a658>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mstudents\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;34m'Apple'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'Banana'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'Cherries'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'Pineapple'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'Turtle'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'Kiwi'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'Tortoise'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mget_random_students\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstudents\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m8\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[0;32m<ipython-input-26-a948e7cb67ca>\u001b[0m in \u001b[0;36mget_random_students\u001b[0;34m(students_list, num)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mget_random_students\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstudents_list\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnum\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m4\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[1;32mreturn\u001b[0m \u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msample\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstudents_list\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnum\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32mC:\\Anaconda3\\lib\\random.py\u001b[0m in \u001b[0;36msample\u001b[0;34m(self, population, k)\u001b[0m\n\u001b[1;32m 315\u001b[0m \u001b[0mn\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpopulation\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 316\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;36m0\u001b[0m \u001b[1;33m<=\u001b[0m \u001b[0mk\u001b[0m \u001b[1;33m<=\u001b[0m \u001b[0mn\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 317\u001b[0;31m \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Sample larger than population or is negative\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 318\u001b[0m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mk\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 319\u001b[0m \u001b[0msetsize\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m21\u001b[0m \u001b[1;31m# size of a small set minus size of an empty list\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[0;31mValueError\u001b[0m: Sample larger than population or is negative" ] } ], "source": [ "students = ['Apple', 'Banana', 'Cherries', 'Pineapple', 'Turtle', 'Kiwi', 'Tortoise']\n", "result = get_random_students(students, 8)\n", "print(result)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def get_random_students_2(students_list, num = 4):\n", " return random.choices(students_list, k = num)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Cherries', 'Pineapple', 'Kiwi', 'Pineapple']\n" ] } ], "source": [ "students = ['Apple', 'Banana', 'Cherries', 'Pineapple', 'Turtle', 'Kiwi', 'Tortoise']\n", "result = get_random_students_2(students)\n", "print(result)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Banana', 'Cherries', 'Banana', 'Tortoise', 'Apple', 'Cherries', 'Cherries', 'Cherries']\n" ] } ], "source": [ "students = ['Apple', 'Banana', 'Cherries', 'Pineapple', 'Turtle', 'Kiwi', 'Tortoise']\n", "result = get_random_students_2(students, 8)\n", "print(result)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def random_mutation(DNA):\n", " position = random.randint(0, len(DNA)-1)\n", " #print(position)\n", " new_base = random.choice('ACTG') \n", " return DNA[:position] + new_base + DNA[position+1:]" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ACTCGGAAGACCCGAGAGCCGAACGGACCGAAGCCGAG\n" ] } ], "source": [ "#DNA = 'ACTGC'\n", "DNA = 'ACTCGGAAGACCCGAGAGCCGAACGGACCGAAGCCCAG'\n", "mutant = random_mutation(DNA)\n", "print(mutant)" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "<class 'bool'>\n" ] } ], "source": [ "changed = (DNA == mutant)\n", "print(changed)\n", "print(type(changed))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def is_mutated(DNA, DNA2):\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "print(is_mutated(DNA, mutant))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "DNA = 'ACTGC'\n", "position = 3\n", "possibles = 'ACT'\n", "#test here first\n", "print(possibles)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def random_mutation_2(DNA):\n", " position = random.randint(0, len(DNA))\n", " possibles = 'ACT'\n", " #add stuff\n", " new_base = random.choice('ACTG') \n", " return DNA[:position] + new_base + DNA[position:]" ] } ], "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.6.0" } }, "nbformat": 4, "nbformat_minor": 2 }