Skip to content

Commit

Permalink
Read from file Answer UCL-RITS#142
Browse files Browse the repository at this point in the history
  • Loading branch information
nuttamas committed Dec 23, 2020
1 parent b25662c commit 8ff5a57
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions average-squares-example/average_squares/squares.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,20 @@ def convert_numbers(list_of_strings):

if __name__ == "__main__":
parser = ArgumentParser(description="Weighted averaged of squared numbers")
parser.add_argument("numbers", type = int, nargs='+', help = "the list of numbers")
parser.add_argument("--weights", "-w", type=int, nargs='+', help="the list of weights")
parser.add_argument("numbers", help = "file containing the list of numbers")
parser.add_argument("--weights", "-w", help="file containing the list of weights")
arguments = parser.parse_args()
numbers = arguments.numbers


# Read number file
with open(arguments.numbers, "r") as input_file_numbers:
numbers_strings = input_file_numbers.readlines()
numbers = convert_numbers(numbers_strings)

# Read and convert weight strings to number, otherwise weight =1
if arguments.weights:
weights = arguments.weights
with open(arguments.weights, "r") as weights_file:
weight_strings = weights_file.readlines()
weights = convert_numbers(weight_strings)
else:
weights = [1] * len(numbers)

Expand Down

0 comments on commit 8ff5a57

Please sign in to comment.