Skip to content

Instantly share code, notes, and snippets.

@ctb
Created October 3, 2012 16:48
Show Gist options
  • Save ctb/3828240 to your computer and use it in GitHub Desktop.
Save ctb/3828240 to your computer and use it in GitHub Desktop.
CSE891 2012 week4 solutions
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "solutions-week4-penny"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": "# Penny needs your help!\n\nLet\u2019s introduce our protoganist, Penny. Penny is a porpoise, and she needs your help.\n\nPenny has a serious problem: her doctor has told her that she has high cholesterol, and wants to know about her diet. He\u2019s guessing that that she needs to eat less ahi and more maguro (she does love her tuna!), but needs to know how many fish she eats each day on average, as well as the different kinds of fish she eats each day. Penny, not having any hands (she\u2019s a porpoise, folks!), needs your help to run the calculation. She\u2019s given you a small data set from the last two weeks of her diet, below; but the doctor wants the numbers going back a year, so Penny wants you to write a program to do the calculation for lots of data. She\u2019s already hired an undergrad to do the data entry into a list for your test data set, so all you have to do is start from a list full o\u2019 fish, below.\n\n(Note: You might also suspect from the below list that Penny is a tad overweight. Shh, she\u2019s sensitive.)\n\n## Question 1: how many different types of fish are there in this list!?\n\nWrite some Python code to calculate the number of different types of fish.\n\n## Question 2: Use a dictionary to calculate the number of times Penny has eaten each type of fish, and print out the results.\n\n## Question 3: Calculate a fat-weighted fish index\n\nThis is a NIST standard calculation, but never mind; what you need to know is that salmon and ahi are high-fat and have a Fatty Index (FI) of 2; cod and sole are really lean, and have a FI of 0.5; and all the other fish have FIs of 1. What is the average FI/day of Penny\u2019s diet?"
},
{
"cell_type": "code",
"collapsed": false,
"input": "fishdiet = ['mackerel', 'salmon', 'halibut', 'mackerel', 'mackerel',\n'plaice', 'halibut', 'halibut', 'salmon', 'halibut', 'tilapia',\n'cod', 'halibut', 'mackerel', 'cod', 'salmon', 'cod', 'sole',\n'plaice', 'tilapia', 'mackerel', 'salmon', 'mackerel', 'cod',\n'mackerel', 'salmon', 'salmon', 'halibut', 'salmon', 'plaice',\n'sole', 'sole', 'ahi', 'mackerel', 'halibut', 'tilapia', 'ahi',\n'halibut', 'salmon', 'sole', 'sole', 'ahi', 'salmon',\n'halibut', 'plaice', 'char', 'sole', 'char', 'char', 'sole',\n'sole', 'sole', 'halibut', 'char', 'plaice', 'mackerel',\n'tilapia', 'halibut', 'maguro', 'maguro', 'plaice', 'maguro',\n'char', 'mackerel', 'halibut', 'maguro', 'plaice', 'maguro',\n'tilapia', 'salmon', 'mackerel', 'maguro', 'cod', 'sole',\n'mackerel', 'salmon', 'maguro', 'mackerel', 'salmon', 'ahi',\n'cod', 'maguro', 'mackerel', 'plaice', 'tilapia', 'tilapia',\n'sole', 'maguro', 'salmon', 'plaice', 'halibut', 'sole',\n'tilapia', 'salmon', 'salmon', 'sole', 'halibut', 'plaice',\n'salmon', 'cod', 'tilapia', 'ahi', 'mackerel', 'mackerel',\n'ahi', 'char', 'ahi', 'maguro', 'mackerel', 'sole', 'maguro',\n'cod', 'mackerel', 'halibut', 'sole', 'plaice', 'sole', 'cod',\n'ahi', 'halibut', 'char', 'halibut', 'cod', 'sole', 'mackerel',\n'plaice', 'sole', 'mackerel', 'tilapia', 'char', 'sole',\n'maguro', 'cod']",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": "### How many types of fish are there in this list?\n\nI will show you two ways to do it --\n\n1. use a dictionary.\n\n2. use a set, a new data structure that you've never seen before :)"
},
{
"cell_type": "code",
"collapsed": false,
"input": "# using a dictionary (Which we'll reuse later)\nfish_d = {}\nfor k in fishdiet:\n # if it's already in fish_d, get the associated count; otherwise, retrieve 0.\n count = fish_d.get(k, 0)\n \n # increment the count\n count += 1\n \n # set in fish_d\n fish_d[k] = count\n \n # note, you could do this in one succinct line of code, like so: fish_d[k] = fish_d.get(k, 0) + 1\n \nprint 'There are', len(fish_d), 'types of fish in Penny\\'s diet.'",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "There are 10 types of fish in Penny's diet.\n"
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": "# using a set, which takes a list and collects only the unique elements.\nprint 'There are', len(set(fishdiet)), 'types of fish in Penny\\'s diet.'",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "There are 10 types of fish in Penny's diet.\n"
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": "## Now, print out the numbers of times Penny has eaten each type of fish"
},
{
"cell_type": "code",
"collapsed": false,
"input": "for k in fish_d:\n print 'Penny ate', fish_d[k], k",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Penny ate 12 plaice\nPenny ate 8 ahi\nPenny ate 10 tilapia\nPenny ate 20 mackerel\nPenny ate 19 sole\nPenny ate 16 salmon\nPenny ate 12 maguro\nPenny ate 8 char\nPenny ate 11 cod\nPenny ate 17 halibut\n"
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": "## Calculate the fat index.\n\nThere are two simple ways to do it -- one, with if statements; the other, with a dict.\n\n(salmon and ahi have an FI of 2; cod and sole have an FI of 0.5; and all the other fish have FIs of 1.)"
},
{
"cell_type": "code",
"collapsed": false,
"input": "# with if statements\nsum_fi = 0.0\nfor k in fish_d:\n count = fish_d[k]\n if k == 'salmon' or k == 'ahi':\n sum_fi += count*2.0\n elif k == 'cod' or k == 'sole':\n sum_fi += count*0.5\n else:\n sum_fi += count * 1.0\n\nprint 'Summed fat index is:', sum_fi",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Summed fat index is: 142.0\n"
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": "# with dicts\nfi_dict = {}\nfi_dict['salmon'] = 2.0\nfi_dict['ahi'] = 2.0\nfi_dict['cod'] = 0.5\nfi_dict['sole'] = 0.5\n\nsum_fi = 0.0\nfor k in fish_d:\n count = fish_d[k]\n fi_factor = fi_dict.get(k, 1.0)\n sum_fi += count * fi_factor\n \nprint 'Summed fat index is:', sum_fi",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Summed fat index is: 142.0\n"
}
],
"prompt_number": 7
},
{
"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