19.10. Chapter Exercises

The problems in these exercises use a new data set. The file "stocks.txt" has data from the Dow Jones Industrial Average which tracks the average performance of a collection of large stocks. Each line of the file represents the opening day of trading (not always the first of the month as the market is not open every day).These lines look like:

3-Dec-01,9848.93,10220.78,9651.87,10021.57

The values on the line are separated by commas and are the following (in order):

To see all of the data, you can use a loop to print out each line.

Below is the start of a program to read in the "stocks.txt" file and run code on each line in the file.

Add code to split the line into a list of values and print out the date value. (The date should be the first value in the list that you create with split.)

Your final output should be a long list of dates and nothing else.

You can’t use codelens with file reading problems, but you can use print statements to check what your code is doing. Feel free to use extra ones while writing your code and Then remove them or comment them out when everything is working.

Modify your program to print out the highest value the Dow Jones reached. (This should be the largest of the monthly high values.)

Tip: When you get the high value, you will need to convert it from a string to a float to work with it as a decimal number. This should look like: float(values[??]).

The final version of your program should only print out the one highest value, but you should work your way up to that. Start by printing out all of the monthly high values, then worry about finding the highest one.

You can’t use codelens with file reading problems, but you can use print statements to check what your code is doing.

Modify your program from question 1 to only print the dates from a specific year specified by a variable desiredYear. If desiredYear is 96, you would only print out values where the year (last part of the date value) is “96”.

You should try changing desired year to different values to make sure your program works for any year for which there is data (89-01), but to pass the tests, you must set desiredYear to “92”.

You can’t use codelens with file reading problems, but you can use print statements to check what your code is doing.

Combine your solutions from problems 2 and 3 and make your program find the largest highest value from the records indicated by the variable desiredYear. I.e. if desiredYear is “96”, your program should only consider the records where the date value ends in “96”, and from those, should find the largest “highest value for month” seen in those records.

You should try changing desired year to different values to make sure your program works for any year for which there is data (89-01), but to pass the tests, you must set desiredYear to “96”.

You can’t use codelens with file reading problems, but you can use print statements to check what your code is doing.

Turn your code from question 4 into a function so we can easily check the max value in multiple years. The function should be called maxHighForYear. It should take the desiredYear and the data as parameters and return the max “highest value for month” found in the records that match the desired year.

The starter code has a simple test of your function followed by a more complex test that tests all the valid years. If you need to debug your code, it might be easier if you comment out the complex test and just run the simple one.

You can’t use codelens with file reading problems, but you can use print statements to check what your code is doing.

Write the function avgVolumeForYear. It should take the desiredYear and the data as parameters and return the average of the “volume” value found in the records that match the desired year. (The “volume” is the last value in each record.)

The starter code has a simple test of your function followed by a more complex test that tests all the valid years. If you need to debug your code, it might be easier if you comment out the complex test and just run the simple one.

You can’t use codelens with file reading problems, but you can use print statements to check what your code is doing.

You have attempted of activities on this page