Programming

MATH 230 : Fall 2023

Department of Mathematics - SUNY Geneseo
⇐ Back

Homework 9 - Reading and Writing Files

Due Date: November 21, 2023

Upload

Recall that for each problem, start your solution by using the characters #%% on the first line of your solution. These three characters create a code block in Spyder. The assignment is due by 11:59 pm on the due date. When opening a text file with the open() function, you should include the encoding='utf-8' keyword argument, for example:
with open('sample_file.txt', 'r', encoding='utf-8') as file_content:
    # do something with file_content

Problems

  1. Read Opening and Closing a File in Python and describe two ways to open and close a file. Which is the preferred way and why? In all your programs, use the preferred way.
  2. When using the open() function to open a file, list at least three different modes that a file can be opened with? What is the default mode?
  3. Write a function that has one input parameter \(N\) which is a positive integer and creates a file named square_N.txt that contains the lines:
    1, 1
    2, 4
    3, 9
    ...
    N, N^2
    Thus, if your function is called with the parameter \(N=5\) then the file created by your function will be named square_5.txt and will contain the lines:
    1, 1
    2, 4
    3, 9
    4, 16
    5, 25
  4. Write a function that has one input parameter which is a list of numbers and creates a file of the following form:
    The length of the input list is \(N_1\)
    The maximum value in the input list is \(N_2\)
    The minimum value in the input list is \(N_3\)
    The average value in the input list is \(N_4\)
    The sum of the values in the input list is \(N_5\)
    where \(N_1, N_2, N_3, N_4, N_5\) are the actual values for the specific input list passed into the function. Obviously, the sum, min, and max functions will be useful for this problem.
  5. The file some_numbers.txt contains a long string consisting of numbers separated by commas. Write a program that reads the file and then creates a list that stores all of the numbers in the file (as floats). Then apply the function from the previous problem to the list of numbers. Note: Recall that when you read in a file, all data is read in as a string data type.
  6. Recall that the distance of a point \((x, y)\) to the origin is given by \(d = \sqrt{x^2+y^2}\). The file points.txt contains on each line \((x,y)\) coordinates of points. Write a program that reads the file (do not copy-paste the data) and computes the distance of each point to the origin and determines which point is furthest away from the origin, which point is closest to the origin, and the average distance to the origin. Your program should print to the screen a summary of your findings in the following form:
    At a distance of \(d_1\), the point closest to the origin is \((x_1, y_1)\)
    At a distance of \(d_2\), the point furthest from the origin is \((x_2, y_2)\)
    The average distance to the origin is \(d_{\text{avg}}\)
    where \(d_1, d_2, (x_1, y_1), (x_2, y_2), d_{\text{avg}}\) should be replaced by the actual values. In your print summary, round all floats to three decimal places. Note: Recall that when you read in a file, all data is read in as a string data type.