Question

In this assignment, you will revisit reading data from a file, and use that data as...

In this assignment, you will revisit reading data from a file, and use that data as arguments (parameters) for a number of functions you will write.

You will need to:

  1. Write and test a function square_each(nums)
    • Where nums is a (Python) list of numbers. It modifies the list nums by squaring each entry and replacing its original value. You must modify the parameter, return will not be allowed!
  2. Write and test a function sum_list(nums)
    • Where nums is a (Python) list of numbers. It returns the sum of the numbers in passed-in the list.
  3. Write and test a function to_numbers(str_list)
    • Where str_list is a list of strings, each of which represents a number. Modify each entry in the list by converting it to a number. You can choose to use return the list, or just modify str_list in-place, similar to #1, above.
  4. Write and test a function main()
    • Take the previous three functions and construct a main() function that will call these functions properly. The program computes the sum of the squares of numbers read from a file. Your program should prompt for a file name and print out the sum of the squares of the values in the file. Hint: use readlines()

You may download two example files of numbers to read in, numbers (space seperated)or numbers (comma seperated). You may choose to read in numbers in another format of your choosing, http://www.pas.rochester.edu/~rsarkis/csc161/_static/misc/numbers.txt, and http://www.pas.rochester.edu/~rsarkis/csc161/_static/misc/numbers_csv.txt so long as you choose one method and document it in your program comments.

Output

This example parses the numbers_csv.txt file. The result should be the same regardless of which input data was used.

This program computes the sum of the squares of numbers read from a file.
Please enter the file name: numbers_csv.txt
The sum of the squares of the numbers in the file is 32932.0

contents of numbers_csv.txt: 54 63 63 42 83 42 22 27 88 52

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the full code, refer the screenshot for the output and code indentation

_________________________________________________________________________________________________

#Write and test a function square_each(nums)
def square_each(nums):
    if nums ==None:
        return
    for i in range(len(nums)):
        nums[i]*=nums[i]

#Write and test a function sum_list(nums)
l=[1,2,3,4,5]
square_each(l)
print(l)

#Write and test a function sum_list(nums)
def sum_list(nums):
    if nums ==None:
        return 0
    summation=0
    for num in nums:
        summation+=num
    return summation

print(sum_list([1,2,3,4,5]))

#Write and test a function to_numbers(str_list)
def to_numbers(str_list):
    for i in range(len(str_list)):
        str_list[i]=int(str_list[i])

str_list=['1','2','3','4','5']
to_numbers(str_list)
print(str_list)

def main():
    file_path='D:\contents.txt'
    with open(file_path,'r') as read_file:
        for line in read_file.readlines():

            if line is None:
                continue
            line=line.strip().split()
            print(line)
            to_numbers(line)
            print('Summation',sum_list(line))
            square_each(line)
            print('Each num squared: ',line)

main()

______________________________________________________________________________________________

thank you !

friend : )

Add a comment
Know the answer?
Add Answer to:
In this assignment, you will revisit reading data from a file, and use that data as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • FUNCTIONS In this assignment, you will revisit reading data from a file, and use that data...

    FUNCTIONS In this assignment, you will revisit reading data from a file, and use that data as arguments (parameters) for a number of functions you will write. You will need to: Write and test a function square_each(nums) Where nums is a (Python) list of numbers. It modifies the list nums by squaring each entry and replacing its original value. You must modify the parameter, a return will not be allowed! Write and test a function sum_list(nums) Where nums is a...

  • This program should use a main function and two other functions named playlist and savelist as...

    This program should use a main function and two other functions named playlist and savelist as follows: The main function: The main function should create an empty list named nums and then use a loop to add ten integers to nums, each integer in the range from 10-90. NOTE: In Python, a list is a data type. See Chapter 7. The main function should then call the playlist function and savelist function, in that order. Both of these functions take...

  • Write a program that performs the following: - Reads from the file "myfile.txt" using readlines() -...

    Write a program that performs the following: - Reads from the file "myfile.txt" using readlines() - Prints out the contents of the file "myfile.txt", that was read into a list by readlines(). Note that this should not look like a list, so you will need to loop through the list created by readlines and print the text. - Use the try/except method to create the file if it does not exist - If the file does not exist, prompt the...

  • (statistics.py) Write a program that reads data from the file provided, data.txt, into a list. Once...

    (statistics.py) Write a program that reads data from the file provided, data.txt, into a list. Once the data are in a list, calculate and print the following: sum = 69.28 mean = 3.46 min = 0.19 max = 8.29 You may use the Python built-ins, min(), max(), and sum(). Format the numbers using '.2f'.

  • This assignment assumes you have completed Programming Assignment 6, Random Number File Writer. Write another program...

    This assignment assumes you have completed Programming Assignment 6, Random Number File Writer. Write another program that reads the random numbers from the random.txt file created in Programming Assignment 6, display the numbers, and then display the following data: The total of the numbers. The number of numbers read from the file. Please program in python

  • --------__--------__Python--------__--------__ You will be reading in the data from the file SalesJan2009.csv. When you do, you...

    --------__--------__Python--------__--------__ You will be reading in the data from the file SalesJan2009.csv. When you do, you need to save all the items from the PRICE field into a list called amtCollected. After you get them all in that list, you will need to create a variable called total that holds the sum of all the numbers in the list. Then create a variable called avg that holds the average of the numbers in the list. Now, print the following strings:...

  • Here is the Prompt for problem 1: Write a C++ program that reads in a test file. The first numbe...

    Here is the Prompt for problem 1: Write a C++ program that reads in a test file. The first number in the file will be an integer, and will indicate the amount of decimal numbers to follow. Once you have the read the numbers from the file into an array then compute the following properties on the set of numbers: the sum, the average (mean), the variance, the standard deviation, and the median. Don’t try to do the entire program...

  • Arrays and reading from a file USE C++ to write a program that will read a...

    Arrays and reading from a file USE C++ to write a program that will read a file containing floating point numbers, store the numbers into an array, count how many numbers were in the file, then output the numbers in reverse order. The program should only use one array and that array should not be changed after the numbers have been read. You should assume that the file does not contain more than 100 floating point numbers. The file name...

  • Use C++ For this week’s lab you will write a program to read a data file...

    Use C++ For this week’s lab you will write a program to read a data file containing numerical values, one per line. The program should compute average of the numbers and also find the smallest and the largest value in the file. You may assume that the data file will have EXACTLY 100 integer values in it. Process all the values out of the data file. Show the average, smallest, largest, and the name of the data file in the...

  • Use Ocaml language writing a bunch of simple functions in a file called warmups.ml. Write these...

    Use Ocaml language writing a bunch of simple functions in a file called warmups.ml. Write these functions in a pure functional style only: no assignment statements, no explicit loops, and no arrays! Write a function to compute fibonacci numbers (in the sequence 0, 1, 1, 2, 3, 5, ... where each number is the sum of the previous two numbers on the list). Use pattern matching but no if/then/else statements. Use a tail recursive solution to make sure the function...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT