Created
September 21, 2012 01:59
-
-
Save ctb/3759374 to your computer and use it in GitHub Desktop.
Week2 HW solutions
This file contains 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
{ | |
"metadata": { | |
"name": "course-02-week2-solutions" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "def volume(r):\n return 2.0/3.0 * 3.141592 * r**3\n\nvolume(5)", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 4, | |
"text": "261.7993333333333" | |
} | |
], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "def triple(x):\n new_x = []\n for j in x:\n new_x.append(j)\n new_x.append(j)\n new_x.append(j)\n return new_x\n\ntriple([3,4,5])", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 5, | |
"text": "[3, 3, 3, 4, 4, 4, 5, 5, 5]" | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "# note, you could also write 'triple' like this:\ndef triple2(x):\n new_x = []\n for j in x:\n new_x.extend([j]*3)\n return new_x\n\ntriple2([3,4,5])", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 6, | |
"text": "[3, 3, 3, 4, 4, 4, 5, 5, 5]" | |
} | |
], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "def uniq(x):\n uniq_x = []\n for j in x:\n if not (j in uniq_x):\n uniq_x.append(j)\n return uniq_x\n\nuniq([3,4,4,4,4,4])\n", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 7, | |
"text": "[3, 4]" | |
} | |
], | |
"prompt_number": 7 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "def uniq_nocase(x):\n uniq_x = []\n for j in x:\n j = j.lower()\n if not (j in uniq_x):\n uniq_x.append(j)\n \n return uniq_x\n\nuniq_nocase(['foo', 'Foo'])", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 8, | |
"text": "['foo']" | |
} | |
], | |
"prompt_number": 8 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "# to put the primes in a list, you just need the tagged lines:\n\ndef primes(N):\n primes_list = [] ## this one\n num = 2\n while num < N:\n is_prime = True\n trial = 2\n while trial**2 <= num:\n if num % trial == 0:\n is_prime = False\n break\n trial += 1\n \n if is_prime:\n primes_list.append(num) ### this one\n num += 1\n\n return primes_list ### and this one\n\nprimes(10)", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 2, | |
"text": "[2, 3, 5, 7]" | |
} | |
], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "## for the prime number sieve, you want to replace the middle loop with one that goes through all the primes collected\n## so far and check them to see if they divide the prime number you are testing.\n##\n## I don't think I defined the problem very clearly -- sorry!\n\ndef primes(N):\n primes_list = []\n num = 2\n while num < N:\n is_prime = True\n \n ## start here\n for r in primes_list:\n if num % r == 0:\n is_prime = False\n break\n \n if is_prime:\n primes_list.append(num)\n num += 1\n\n return primes_list\n\nprimes(10)", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 3, | |
"text": "[2, 3, 5, 7]" | |
} | |
], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment