Question: Computing 7 Assignment Background In this assignment, you will be extracting daily temperature data, sorting the data, and then calculating the median temperature for a
Computing Assignment
Background
In this assignment, you will be extracting daily temperature data, sorting the data, and then calculating the median temperature for a given set of data.
A set of daily temperature data will be provided to you as a list of strings. For example:
temps "None","NA
Note that the temperature may not be recorded on some days. A sequence of characters that cannot be represented as a number indicate that the temperature was not recorded on a given day. In the previous example, we can see that the strings "None" and NA were used to represent days where the temperature was not recorded. It is important to note that these strings can consist of any characters and can vary in length. Some more examples would be the empty string "Nothing", "Not recorded", "Forgot", etc.
To calculate the median temperature for a given set of data, we first need to convert the data to a list of numbers. The data converted from the temps list is given below. Notice how the days that did not have a recorded temperature were ignored:
tempsextracted
How would we calculate the median of our list? Recall that the median is a value separating the higher half from the lower half of a list of sorted values. In the case where the length of our list of numbers is odd, the median is the middle element in our list. In the case where the length of our list of numbers is even, the median can be found by taking the average of the two center most elements.
We cannot calculate the median until we sort our values. Our sorted temperatures will look as follows:
tempssorted
Now that our list is sorted, we can see that the median in this case is
So how do we sort a list of values? Formally, a sorting algorithm is a sequence of steps that are used to sort a sequence of values. It turns out that there exist many different sorting algorithms, some more complicated than others. In todays assignment we are going to focus on an intuitive sorting algorithm known as selection sort. We are going to use this algorithm to sort a list of numbers in ascending order.
Say we have a list named nums. The main idea of selection sort is that we want to start iterating over our list nums from i to the length of our list minus n choosing the smallest element in the sub list from numsi to numsn and placing it in numsi on each iteration. If you are confused, don't worry! We have provided you the pseudocode for the algorithm. Before looking at the pseudocode, use the following example to gain some intuition about how selection sort works.
Imagine that we have the following list of numbers that need to be sorted:
algpng
We start sorting at the left most item. We call our current position index i The left most element in our list is the number which occurs at i
algpng
When considering the final sorted list, should the number be in this position? What is the smallest element in our list from index i to the end of our list index By inspection, we can see that the smallest number is We can identify the position of this number using the variable minindex, which in this case is minindex Therefore the number should be at index i because it is the smallest element in our list from index to index
algpng
We can guarantee that the number will be in the correct position in our final sorted list if we swap the elements at i and minindex!
algpng
You might think that is still not in its correct position, but don't worry! We can guarantee that the number is in the correct position, and we will worry about later.
Let's now increment i to algpng
Let's perform the same actions we did when i was What is the smallest element in our list from the element at this position i to the end of our list index By inspection, we can see that the smallest number is which occurs at index thus minindex in this case.
algpng
We then swap the elements at i and minindex!
algpng
Let's now increment i once again to
algpng
Can you see what's going on here? All of the elements green to the left of our current index i are sorted! Therefore our entire list will be sorted if we continue with
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
