" '''Normalizes a rectangle so that it is at the origin and 1.0 units long on its longest axis.'''\n",
" assert len(rect) == 4, 'Rectangles must contain 4 coordinates'\n",
" llx, llyy, urx, ury = rect\n",
" llx, lly, urx, ury = rect\n",
" assert llx < urx, 'Invalid X coordinates'\n",
" assert lly < ury, 'Invalid Y coordinates'\n",
"\n",
...
...
@@ -141,6 +141,27 @@
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Post-conditions in lines 17, 18 check well formed output:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
...
...
@@ -244,20 +265,6 @@
"# Exercises"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pre- and Post-Conditions\n",
"\n",
"Suppose you are writing a function called average that calculates the average of the numbers in a list. What pre-conditions and post-conditions would you write for it? Compare your answer to your neighbor’s: can you think of a function that will pass your tests but not his/hers or vice versa?"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
...
...
@@ -286,12 +293,17 @@
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pre- and Post-Conditions\n",
"\n",
"Suppose you are writing a function called average that calculates the average of the numbers in a list. What pre-conditions and post-conditions would you write for it? Compare your answer to your neighbor’s: can you think of a function that will pass your tests but not his/hers or vice versa?"
"We found some suspicious features in our firs dataset \"inflammation-01.csv\". It is possible that other datasets are also affected by this?? We would like to check... Fast!"
"We found some suspicious features in our first dataset \"inflammation-01.csv\". It is possible that other datasets are also affected by this?? We would like to check... Fast!"
]
},
{
...
...
@@ -133,22 +133,6 @@
"<li>etc.."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 3. This loop overrides the value of a external variable:"
"In last section we discovered something suspicios in our inflammation data by drawing some plots. How can we use Python\n",
"In last section we discovered something suspicious in our inflammation data by drawing some plots. How can we use Python\n",
"to automatically detect the anomalies we found and take an action for each of them?"
]
},
...
...
@@ -38,7 +38,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"This scheme is achieved in Python by means of the <code>if</code>/<code>else</code> clause:"
"This scheme is achieved in Python by means of the <code>if</code>/<code>else</code> clause. If there are multiple choices we make use of the <code>elif</code>:"
]
},
{
...
...
@@ -54,25 +54,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Sometimes there is no need to use a else, it depends on the logic of the problem:"
"<strong>Note the \"==\"</strong>. This symbol checks equality rather than \"=\" wich means assingment"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sometimes the flow of the decision is more complex. There are still multiple choices available \n",
"when the first condition is discarded. <br>\n",
"As an example imagine we want to know if a given number is positive negative or zero. We make use of the <code>elif</code> clause:"
"Sometimes there is no need to use a else, it depends on the logic of the problem:"
]
},
{
...
...
@@ -84,13 +73,6 @@
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<strong>Note the \"==\"</strong>. This symbol checks equality rather than \"=\" wich means assingment"
]
},
{
"cell_type": "markdown",
"metadata": {},
...
...
@@ -198,6 +180,20 @@
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
...
...
@@ -213,15 +209,6 @@
"First two files max inflammation seems to rise linearly. So let's write a code that check this behavior and show warning:"
"Suppose you are writing a function called average that calculates the average of the numbers in a list. What pre-conditions and post-conditions would you write for it? Compare your answer to your neighbor’s: can you think of a function that will pass your tests but not his/hers or vice versa?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# a possible pre-condition:\n",
"assert len(input) > 0, #'List length must be non-zero'\n",
"# a possible post-condition:\n",
"assert numpy.min(input) <= average <= numpy.max(input), #'Average should be between min and max of input values'"
]
},
{
"cell_type": "markdown",
"metadata": {},
...
...
@@ -394,10 +371,8 @@
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": true
},
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import numpy\n",
...
...
@@ -409,6 +384,7 @@
" return None\n",
" lowest, highest = ranges[0]\n",
" for (low, high) in ranges[1:]:\n",
" assert low <= high, 'first element in range should be the lower one'\n",
" lowest = max(lowest, low)\n",
" highest = min(highest, high)\n",
" if lowest >= highest: # no overlap\n",
...
...
@@ -419,23 +395,33 @@
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": true
},
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"test_range_overlap()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pre- and Post-Conditions\n",
"\n",
"Suppose you are writing a function called average that calculates the average of the numbers in a list. What pre-conditions and post-conditions would you write for it? Compare your answer to your neighbor’s: can you think of a function that will pass your tests but not his/hers or vice versa?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": []
"source": [
"# a possible pre-condition:\n",
"assert len(input) > 0, #'List length must be non-zero'\n",
"# a possible post-condition:\n",
"assert numpy.min(input) <= average <= numpy.max(input), #'Average should be between min and max of input values'"
"We found some suspicious features in our firs dataset \"inflammation-01.csv\". It is possible that other datasets are also affected by this?? We would like to check... Fast!"
"We found some suspicious features in our first dataset \"inflammation-01.csv\". It is possible that other datasets are also affected by this?? We would like to check... Fast!"
]
},
{
...
...
@@ -39,15 +39,15 @@
"name": "stdout",
"output_type": "stream",
"text": [
"l\n",
"e\n",
"t\n",
"a\n",
"d\n"
"n\n",
"k\n"
]
}
],
"source": [
"word = 'lead' # From wich number to wich number we had to go? Ask people\n",
"word = 'tank' # From wich number to wich number we had to go? Ask people\n",
"This scheme is achieved in Python by means of the <code>if</code>/<code>else</code> clause. If there are multiple choices we make use of the <code>elif</code>:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<strong>Note the \"==\"</strong>. This symbol checks equality rather than \"=\" wich means assingment"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sometimes there is no need to use a else, it depends on the logic of the problem:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercises\n",
"#### What Is Truth?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"if '':\n",
" print('empty string is true')\n",
"if 'word':\n",
" print('word is true')\n",
"if []:\n",
" print('empty list is true')\n",
"if [1, 2, 3]:\n",
" print('non-empty list is true')\n",
"if 0:\n",
" print('zero is true')\n",
"if 1:\n",
" print('one is true')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### That’s Not Not What I Meant"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"if not '':\n",
" print('empty string is not true')\n",
"if not 'word':\n",
" print('word is not true')\n",
"if not not True:\n",
" print('not not True is true')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Composed conditions\n",
"We can write complex conditions in the if clause by using concatenation of simple conditions by means of <code>and</code> and <code>or</code>. <br>\n",
"As in human language an <code>and</code> condition is True if all its components are True:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Whereas an <code>or</code> condition is True if any of it components is True:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise\n",
"\n",
"#### Close Enough\n",
"\n",
"Write some conditions that print True if the variable a is within 10% of the variable b and False otherwise. Compare your implementation with your partner’s: do you get the same answer for all possible pairs of numbers?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Checking our Data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we are in conditions to check our suspicious data in the inflammation tests.<br>\n",
"First two files max inflammation seems to rise linearly. So let's write a code that check this behavior and show warning:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We saw a different issue in file 3. All minima where 0. So we write a code to check that:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can check now several files putting all toghether:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Additional Exercises\n",
"\n",
"### In-place operators"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"Python (and most other languages in the C family) provides in-place operators that work like this:\n",
"\n",
"```x = 1 # original value\n",
"x += 1 # add one to x, assigning result back to x\n",
"x *= 3 # multiply x by 3\n",
"print(x)```\n",
"\n",
"\n",
"``6``\n",
"\n",
"Write some code that sums the positive and negative numbers in a list separately, using in-place operators. Do you think the result is more or less readable than writing the same without in-place operators?\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Counting Vowels "
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"* Write a loop that counts the number of vowels in a character string.\n",
"* Test it on a few individual words and full sentences.\n",
"* Once you are done, compare your solution to your neighbor’s. Did you make the same decisions about how to handle the letter ‘y’ (which some people think is a vowel, and some do not)?\n"
"In last section we discovered something suspicios in our inflammation data by drawing some plots. How can we use Python\n",
"In last section we discovered something suspicious in our inflammation data by drawing some plots. How can we use Python\n",
"to automatically detect the anomalies we found and take an action for each of them?"
]
},
...
...
@@ -38,30 +38,40 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"This scheme is achieved in Python by means of the <code>if</code>/<code>else</code> clause:"
"This scheme is achieved in Python by means of the <code>if</code>/<code>else</code> clause. If there are multiple choices we make use of the <code>elif</code>:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"not greater\n",
"done\n"
"-3 is negative\n"
]
}
],
"source": [
"num = 37\n",
"if num > 100:\n",
" print('greater')\n",
"\n",
"num = -3\n",
"\n",
"if num > 0:\n",
" print(num, 'is positive')\n",
"elif num == 0: # note the sign == instead of =. The reason is that \"==\" tests equality whereas \"=\" means assignment\n",
" print(num, 'is zero')\n",
"else:\n",
" print('not greater')\n",
"print('done')"
" print(num, 'is negative')\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<strong>Note the \"==\"</strong>. This symbol checks equality rather than \"=\" wich means assingment"
]
},
{
...
...
@@ -93,48 +103,6 @@
"print('... after conditional')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sometimes the flow of the decision is more complex. There are still multiple choices available \n",
"when the first condition is discarded. <br>\n",
"As an example imagine we want to know if a given number is positive negative or zero. We make use of the <code>elif</code> clause:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-3 is negative\n"
]
}
],
"source": [
"\n",
"num = -3\n",
"\n",
"if num > 0:\n",
" print(num, 'is positive')\n",
"elif num == 0: # note the sign == instead of =. The reason is that \"==\" tests equality whereas \"=\" means assignment\n",
" print(num, 'is zero')\n",
"else:\n",
" print(num, 'is negative')\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<strong>Note the \"==\"</strong>. This symbol checks equality rather than \"=\" wich means assingment"
]
},
{
"cell_type": "markdown",
"metadata": {},
...
...
@@ -276,7 +244,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"outputs": [
{
...
...
@@ -298,7 +266,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"metadata": {},
"outputs": [
{
...
...
@@ -315,34 +283,19 @@
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"multiplication=a*b\n",
"substraction=a-b"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Variable Type Data/Info\n",
"-----------------------------------\n",
"a float 0.9\n",
"b int 1\n",
"multiplication float 0.9\n",
"substraction float -0.09999999999999998\n"
"0.9 1 -0.09999999999999998\n"
]
}
],
"source": [
"%whos"
"print(a,b,a-b)"
]
},
{
...
...
@@ -360,31 +313,6 @@
"First two files max inflammation seems to rise linearly. So let's write a code that check this behavior and show warning:"