Question

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:

  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, a 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 separated) or numbers (comma separated). You may choose to read in numbers in another format of your choosing, so long as you choose one method and document it in your program comments.

- make sure your program flexible enough to read in data that is separated by either -commas, or spaces.

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
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question, here is the code in Python. It would have been great if you would have shared the sample input files.

Nevertheless, I have made it flexible so it will work for both space and comma delimeters.

Hope it helps, thanks, let me know for any help with any other question.

=================================================================

def square_each(nums):
    for i in range(len(nums)):
        nums[i] = nums[i] ** 2


def sum_list(nums):
    total=0
    for i in range(len(nums)):
        total+=nums[i]
    return total

def to_numbers(str_list):

    for i in range(len(str_list)):
        try:
            str_list[i] = float(str_list[i])
        except:
            pass



def
main():
    filename = input('Please enter the file name: ')
  str_list=[]
    with open(filename,'r') as infile:
        for line in infile.readlines():
            if ',' in line:
                line = line.strip().split(',')
            else:
                line = line.strip().split()
            for val in line: str_list.append(val.strip())

    to_numbers(str_list)
    square_each(str_list)
    total = sum_list(str_list)
    print('The sum of the squares of the numbers in the file is {}'.format(total))

main()

=================================================================

Code Screenshot

def square_each (nums): for i in range (len (nums)): nums[i] = nums[i] ** 2 def sum_list (nums): total=0 for i in range (len

Add a comment
Know the answer?
Add Answer to:
FUNCTIONS In this assignment, you will revisit reading data from a file, and use that data...
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
  • 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: 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! Write and test a function sum_list(nums) Where nums is a (Python) list...

  • 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...

  • (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'.

  • 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...

  • --------__--------__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:...

  • Please Use C++ for coding . . Note: The order that these functions are listed, do...

    Please Use C++ for coding . . Note: The order that these functions are listed, do not reflect the order that they should be called. Your program must be fully functional. Submit all.cpp, input and output files for grading. Write a complete program that uses the functions listed below. Except for the printodd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention...

  • 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

  • C++ 3. Write a program that reads integers from a file, sums the values and calculates...

    C++ 3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning function that opens an input file named in File txt. You may "hard-code" the file name, i.e., you do not need to ask the user for the file name. The function should check the file state of the input file and return a value indicating success or failure. Main should check the returned value and if the file...

  • C++ Quesetion In this exercise, you are to modify the Classify Numbers programming example in this...

    C++ Quesetion In this exercise, you are to modify the Classify Numbers programming example in this chapter. As written, the program inputs the data from the standard input device (keyboard) and outputs the results on the standard output device (screen). The program can process only 20 numbers. Rewrite the program to incorporate the following requirements: a. Data to the program is input from a file of an unspecified length; that is, the program does not know in advance how many...

  • This lab will combine reading data from a file and searching the array to find a...

    This lab will combine reading data from a file and searching the array to find a specific value. Assignment Write a program that reads in a file full of strings into an array, and prompts the user for a string to find in the array. The program should loop until a sentinel value (such as -1) is entered. After looping in main() for the input, write a search function, with the following prototype: int findWord(string [], int, string); with arguments...

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