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 (Python) list of numbers. It returns the sum of the numbers in passed-in the list.
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.
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
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](http://img.homeworklib.com/questions/c5030320-ef68-11eb-a977-1fadb3e74fc5.png?x-oss-process=image/resize,w_560)
FUNCTIONS In this assignment, you will revisit reading data from a file, and use that data...
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 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 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 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 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 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 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
Copy the following Python fuction discussed in class into your file: from random import * def makeRandomList(size, bound): a = [] for i in range(size): a.append(randint(0, bound)) return a a. Rename the function sumList as meanList and modify it so that it finds the average of the list. The average is the sum divided by the size (len) of the list. Make sure that the function doesn't give you an error when it is called on an empty list. The...
Write a program in C that creates an array of 100 random numbers from 0-99. The program sums the random numbers and prints the sum. It then writes the numbers to a new file using open, close and write. Then looks in the current directory for files that match the pattern “numbers.XXXX”. For each file, open the file and read the file. You can assume that the file will contain 100 integers. Sum the integers. Print the filename and the sum...
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...