Commit e79da4d8 authored by Toby Hodges's avatar Toby Hodges

replaced debugging exercise at end of chapter 2

parent 56af5be2
......@@ -1084,13 +1084,26 @@
"\n",
"__Description:__\n",
"\n",
"The function `max_of_two` takes a list of tuples, with each member tuple containing two numbers each. The function allocates each number to either a 'Low' or 'High' category, in a pre-defined dictionary, and then returns this dictionary containing the new input. There should be no repeated values in each category. Finally, the code should print formatted strings containing the high and low values collected. \n",
"The function `find_within_range` takes a list of numbers as input and finds all the numbers therein that fall between `upper` (default: 0) and `lower` (default: 10). The numbers within the range should be returned as a list. These should be no duplicate entries in the output i.e. if the same number occurs twice in the input, and if its value is within the specified range, it will only appear once in the output.\n",
"\n",
"Output expected:\n",
"\n",
"`The high values are [0.026, 1.09, 0.953, 3.09, 0.943, 0.426, 1.29]`\n",
"\n",
"`The low values are [0.023, 0.666, 0.346, 0.022, 0.336, 0.076, 0.356, 0.146]`"
"Fix the bugs in the function definition. Some test examples are given in the subsequent cell, with expected output given in the comments."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def find_within_range(list_of_numbers, lower=0, upper=10):\n",
" output = {}\n",
" for number in list_of_numbers:\n",
" if 0 > number <= 10:\n",
" if number in output_list:\n",
" output.append(number)\n",
" return output"
]
},
{
......@@ -1101,39 +1114,30 @@
},
"outputs": [],
"source": [
"# define variables to run function on\n",
"list0 = [ (0.023, 0.026), (1.09, 0.666) (0.346, 0.953), (0.022, 0.026), (3.09, 0.666), (0.336, 0.943), (0.076, 0.426), (1.29, 0.356), (0.146, 0.953) ]\n",
"\n",
"# define function \n",
"def max_of_two(nmrs_list_list)\n",
" \n",
" # define dict with assessment values\n",
" assessment_values = { 'Low': {}, 'High'; []}\n",
" \n",
" # run through elements (lists) in input list and categorise elements\n",
" for item in nmrs_list_list:\n",
" a = item(0)\n",
" b = item[1]\n",
" if a < b:\n",
" # add a to low and b to high (no repeats)\n",
" if a not in assessment_values['Low']:\n",
" assessment_values['Low'].append(a)\n",
" if b not in assessment_values['High']:\n",
" assessment_values['High'].append(b)\n",
" else\n",
" # add a to high and b to low (no repeats)\n",
" if a not in assessment_values[High]:\n",
" assessment_values[High].append(a)\n",
" if b not in assessment_values['Low']:\n",
" assessment_values['Low'].append(b)\n",
" return assessment_values\n",
" \n",
"# run function on all variables \n",
"assessment_vals = max_of_two(list0)\n",
" \n",
"# get output of code \n",
"print('The high values are ()'.format(assessment_vals['High']))\n",
"print('The low values are {}'.format(assessment_vals['Low']))\n"
"print(find_within_range([-2, 14, 9, 3.14])) # should return [9, 3.14]\n",
"print(find_within_range([0, 5, 10, 15])) # should return [0, 5, 10]\n",
"print(find_within_range([2.104, 10000, -435, 2.104])) # should return [2.104]\n",
"print(find_within_range([1, 2, 3, 4], lower=2, upper=6)) # should return [2, 3, 4]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"___Optional:___ If you would like to really challenge yourself, try changing the (fixed) function to __return only the smallest three numbers seen__, which still fall within the specified range. Some more examples, with comments on expected output, given below. The order of the numbers in the output list is unimportant."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(find_within_range([-2, 14, 3, -9, 9, 6, 7])) # should return [3, 6, 7]\n",
"print(find_within_range([0, 6, 5, 15, 5, 6])) # should return [0, 6, 5]\n",
"print(find_within_range([1.2, 1.4, 7.8, 4.0, 8.3], lower=1, upper=8)) # should return [1.2, 1.4, 4.0] "
]
}
],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment