984 views
21 votes
Write a program that computes and prints the average of the numbers in a text file. You should make use of two higher-order functions to simplify the design.

User Matthew H
by
7.4k points

1 Answer

3 votes

Answer:import functools

# open your file

file = open("integers.txt", 'r')

file = file.read()

# put numbers into a list

file = file.split()

# convert list into integers

file = list(map(int, file))

# use lambda function to get average.

print(functools.reduce(lambda x, y: x+y / len(file), file, 0))

Step-by-step explanation:

User Meanne
by
6.4k points