From ddac454a430586b51a1c65b510710543911c19e2 Mon Sep 17 00:00:00 2001 From: Eva Ball Date: Thu, 6 Oct 2022 21:21:55 -0400 Subject: [PATCH 1/2] learning doc --- 05_docstring/submissions/ball133.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 05_docstring/submissions/ball133.py diff --git a/05_docstring/submissions/ball133.py b/05_docstring/submissions/ball133.py new file mode 100644 index 0000000..9050ed7 --- /dev/null +++ b/05_docstring/submissions/ball133.py @@ -0,0 +1,27 @@ +import numpy as np + + +def afunc(array1: np.ndarray, array2: np.ndarray, mode: str = "+", pow: int = 2) -> np.ndarray: + """Sums two matrices, then exponentiates each element by pow power. + + Args: + array1: A numpy.ndarray. + array2: A numpy.ndarray. + mode: A string representing '+' or '-' matrix operations. + pow: An integer representing the power. + + Returns: + A numpy.ndarray matrix, summing the passed matrices and exponentiating each element by pow. + """ + assert array1.shape == array2.shape, "size mismatch" + assert type(pow) == int, "pow should be type int" + + if mode == "+": + result = array1 + array2 + elif mode == "-": + result = array1 - array2 + else: + raise ValueError(f"mode is either '+' or '-' but got {mode}") + + result = result**pow + return result From 79a8081cce0b5586a3d96aff9796906dc4274d10 Mon Sep 17 00:00:00 2001 From: Eva Ball Date: Wed, 12 Oct 2022 14:48:08 -0400 Subject: [PATCH 2/2] learning arg --- 06_argparser/submissions/ball133.py | 53 +++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 06_argparser/submissions/ball133.py diff --git a/06_argparser/submissions/ball133.py b/06_argparser/submissions/ball133.py new file mode 100644 index 0000000..49a7580 --- /dev/null +++ b/06_argparser/submissions/ball133.py @@ -0,0 +1,53 @@ +import numpy as np + + +def afunc(array1, array2, mode="+", pow=2): + assert array1.shape == array2.shape, "size mismatch" + assert type(pow) == int, "pow should be type int" + + if mode == "+": + result = array1 + array2 + elif mode == "-": + result = array1 - array2 + else: + raise ValueError(f"mode is either '+' or '-' but got {mode}") + + result = result**pow + return result + + +if __name__ == "__main__": + import argparse + + parser = argparse.ArgumentParser(description="This is tutorial for argparse.") + # add your code here + parser.add_argument( + "mode", + metavar="M", + help=""" + mode defines which summation operation to perform on matrices + and is limited to '+' or '-' + """, + default="+", + type=str, + ) + + parser.add_argument( + "pow", + metavar="P", + help="pow raises each element in the matrix to specified power", + default=2, + type=int, + ) + + parser.add_argument("print_hello", action="store_true") + # add your code here + args = parser.parse_args() + print(args) + + a = np.array([1, 2, 3]) + b = np.array([4, 5, 6]) + result = afunc(a, b, args.mode, args.pow) + print(result) + if args.print_hello: + print("Hello!")