Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Numbers to be averaged now taken as command-line argument #154

Open
wants to merge 2 commits into
base: week06
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Binary file not shown.
19 changes: 10 additions & 9 deletions week06/average-squares-example/average_squares/squares.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Computation of weighted average of squares."""

from argparse import ArgumentParser

def average_of_squares(list_of_numbers, list_of_weights=None):
""" Return the weighted average of a list of values.
Expand All @@ -12,7 +13,7 @@ def average_of_squares(list_of_numbers, list_of_weights=None):
>>> average_of_squares([1, 2, 4])
7.0
>>> average_of_squares([2, 4], [1, 0.5])
6.0
8.0
>>> average_of_squares([1, 2, 4], [1, 0.5])
Traceback (most recent call last):
AssertionError: weights and numbers must have same length
Expand All @@ -29,7 +30,7 @@ def average_of_squares(list_of_numbers, list_of_weights=None):
for number, weight
in zip(list_of_numbers, effective_weights)
]
return sum(squares)
return sum(squares)/sum(effective_weights)


def convert_numbers(list_of_strings):
Expand All @@ -38,7 +39,7 @@ def convert_numbers(list_of_strings):
Example:
--------
>>> convert_numbers(["4", " 8 ", "15 16", " 23 42 "])
[4, 8, 15, 16]
[4.0, 8.0, 15.0, 16.0, 23.0, 42.0]

"""
all_numbers = []
Expand All @@ -51,12 +52,12 @@ def convert_numbers(list_of_strings):


if __name__ == "__main__":
numbers_strings = ["1","2","4"]
weight_strings = ["1","1","1"]

numbers = convert_numbers(numbers_strings)
weights = convert_numbers(weight_strings)
parser = ArgumentParser(description="Average of squares")
parser.add_argument('numbers', action='append', nargs='+')
arguments= parser.parse_args()

numbers = convert_numbers(arguments.numbers[0])

result = average_of_squares(numbers, weights)
result = average_of_squares(numbers)

print(result)