Section 31.4 What’s the largest pollution values?
Now that we have figured out how to process the data in the file, we can get to work on answering questions about it. One of the interesting questions to explore might be, “Which city has the worst pollution at the PM2.5 level?” The program below figures that out. It uses the maximum value pattern to do this - max25
will be used to store the highest PM2.5 value we have seen. We also need to remember the name of the city that value was from. We will use maxCity
to do this and update it every time we find a new max25
. It isn’t easy to initialize these to match the first record, so we will start max25
with a tiny value we know will get replaced by the first record.
There is one extra wrinkle we need to worry about. Recall that the data in a text file is stored as ASCII text. So a PM2.5 value in the file would be "12"
instead of the number 12
. That is a problem because when you compare strings, they are compared alphabetically. "B"
is greater than "Apple"
because B comes after A. By the same logic, the string "2"
is greater than the string "12"
because 2 is greater than 1. If we convert the strings to numbers, things will work as expected - 2
is smaller than 12
. So we must use int()
to convert the PM2.5 values to integer numbers.
Checkpoint 31.4.1.
Modify the code to find the city with the highest PM10 value. Which statement is true?
The same city has the maximum value for both PM 2.5 and PM 10.
Fresno, CA is the worst for both.
Different cities have the maximums, but they are in the same state.
Check that are using values[1] to find the worst PM 10.
Different cities in different states have the maximum values.
You did not actually run and change the program, did you?
Checkpoint 31.4.2.
You have attempted
of
activities on this page.