Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions squares.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
"""Computation of weighted average of squares."""


import argparse
import os



def average_of_squares(list_of_numbers, list_of_weights=None):
""" Return the weighted average of a list of values.

Expand Down Expand Up @@ -51,12 +56,25 @@ def convert_numbers(list_of_strings):


if __name__ == "__main__":
numbers_strings = ["1","2","4"]
weight_strings = ["1","1","1"]
parser = argparse.ArgumentParser(description="Inputs for weighted average of squares")
parser.add_argument("numbers_file", help="File containing numbers")
parser.add_argument("--weights", dest="weights_file", help="File containing weights", default=None)
arguments = parser.parse_args()

numbers = convert_numbers(numbers_strings)
weights = convert_numbers(weight_strings)
number = []
with open(arguments.numbers_file, "r") as numbers_file:
for line in numbers_file:
number.append(float(line.strip()))

if arguments.weights_file is not None:
weights = []
with open(arguments.weights_file, "r") as weights_file:
for line in weights_file:
weights.append(float(line.strip()))

else:
weights = None

result = average_of_squares(numbers, weights)
result = average_of_squares(number, weights if weights else None)

print(result)